changes according to feedback

Changes according to the feedback have been made:

- What is a shell section has been moved before Installation and Start section

- Content changes have been made as suggested in both of the above sections.
This commit is contained in:
Sabine Maennel 2018-08-15 07:47:09 +02:00 committed by ridiculousfish
parent f6974e5a76
commit 20c51b7da9

View file

@ -34,6 +34,123 @@ So shells serve as user-interface between applications and the operating system.
<a href="#introduction"><-</a><hr>
\section shell What is a Shell
This section is about the basics of shells. *fish* is a shell and shells have a lot in common:
- <a href="#shell-standards">Shell Standards</a>: Why shells adjust to standards
- <a href="#man-page">Manual Pages</a>: Commands usually come with a standardized manual page
- <a href="#shell-syntax">Command Syntax</a>: Shell commands have a standard syntax
- <a href="#shell-programming">Commands versus Programs</a>: Commands differ from normal programs
- <a href="#shebang-line">Shebang Line</a>: How the shell knows the language of a script
\subsection shell-standards Shell Standards
A shell is an interface to the operating system that reads from the commandline of a terminal. A shell's task is to identify and interpret commands. The commands can come from different applications and can be written in different programming languages.
This can only work smoothly if shells adapt to some common standards. For shells there is the POSIX standard, see <a href="https://en.wikipedia.org/wiki/Command-line_interface#Command-line_interpreter">Command-line interpreters</a>. `fish` tries to satisfy the POSIX standard wherever it does not get into the way of its own design principles, see <a href="design.html">`fish` Design</a>.
\subsection man-page Manual Pages
There is a common standard on how to receive help on shell commands: applications provide a manual page to their commands that can be opened with the `man` command:
\fish
>man COMMAND
\endfish
This convention helps to make sure help can be found on commands no matter where they originate from. *fish*'s internal commands all come with a manual page.
\subsection shell-syntax Command Syntax
Shells also support some common syntax for executing commands. That way a command can be started in the same way, regardless of the application, where it comes from, and the shell, where it is executed in.
The pattern below is a basic pattern:
\fish
COMMAND [OPTIONS] ARGUMENTS
\endfish
- <b>`COMMAND`</b>: the name of the executeable
- <b>`[OPTIONS]`</b>: options can change the behaviour of a command
- <b>`ARGUMENTS`</b>: input to the command
For external <b>commands</b> the executable is usually stored on file and the file path must be included in the variable `$PATH`, so that the file can be found: see <a href="#variables-special">Special Variables</a>.
<b>Options</b> come in two forms: a short name, that is a hyphen with a single letter; or a long name, consisting of two hyphens with words connected by hyphens. Example: `-h` and `--help` are often used to print a help message. Options are a fixed set, described in the manual pages of the command, see <a href="#man-page">Manual Pages</a>
<b>Arguments</b> are the arbitrary input part of a command: often it is a file or directory name, sometimes it is a string or a list.
Example:
\fish
>echo -s Hallo World!
HalloWorld!
\endfish
- both `Hallo` and `World!` are arguments to the echo command
- `-s` is an option that suppresses spaces in the output of the command
\subsection shell-programming Commands versus Programs
<b>Programs</b> in other languages can often be regarded as black boxes: they get complex input and return complex output. Sometimes they produce side effects such as writing to a file or reporting an error, but the emphasis is on: arguments in and return values out:
Arguments &rarr; |Program| &rarr; Return Values
<b>Shell commands</b> are different:
- the side effects take the center of the stage: they transform an <b>input stream of data</b> into an <b>output stream of data</b>. Both of these streams are usually the terminal, but they can be redirected.
- the arguments become options or switches paired with data: the switches influence the behaviour of the command
- the return value shrinks to an <b>exit code</b>: this exit code is 0 when the command executes normally and between 1 and 255 otherwise.
<table with=100%>
<tr><td>Input Stream &rArr;<td rowspan="2">|Shell Command|<td>&rArr; Output Stream
<tr><td>switch and data as arguments &rarr;<td>&rarr; exit code
</table>
This leads to another way of programming and especially of combining commands:
There are two ways to combine shell commands:
- Commands can pass on their streams to each other: one command takes the output stream of another command as its input stream and the two commands execute in parallel. This is called piping, see <a href="#piping">Piping</a>.
Example:
\fish
# Every line of the `ls` command is immediatelly passed on to the `grep` command
>ls -l | grep "my topic"
\endfish
- Commands can pass on all their output as a chunk</b>: the output stream of one command is bundled and taken as data argument for the second command. This is called command substitution, see <a href="#expand-command-substitution">Command Substitution</a>.
Example:
\fish
# the output of the inner `ls` command is taken as the input argument for the outer `echo` command
>echo (ls a*)
\endfish
\subsection shebang-line Shebang Line
Since script for shell commands can be written in many different languages, they need to carry information about what interpreter is needed to execute them: For this they are expected to have a first line, the shebang line, which names an executable for this purpose:
Example:
A scripts written in `bash` it would need a first line like this:
```
#!/bin/bash
```
- this line tells the shell to execute the file with the *bash* interpreter, that is located at the path `/bin/bash`.
For a script, written in another language, just replace the interpreter `/bin/bash` with the language interpreter of that other language (for example `/bin/python` for a `python` script)
This line is only needed when scripts are executed by another interpreter, so for *fish* internal commands, that are executed by *fish* the shebang line is not necessary.
<a href="#shell"><-</a><hr>
\section install Installation and Start
This section is on how to install, uninstall, start and exit a *fish* shell and on how to make *fish* the default shell:
@ -80,139 +197,20 @@ Once *fish* has been installed, open a terminal. If *fish* is not the default sh
\subsection bash Executing Bash
If *fish* is your default shell you can still execute *bash* commands in case you need to:
If *fish* is your default shell and you want to copy commands from the internet, that are written in a different shell language, *bash* for example, you can proceed in the following way:
- a single command can be executed with:
Consider, that `bash` is also a command. With `man bash` you can see that there are two ways to do this:
- `bash` has a switch `-c` to read from a string:
\fish
>bash -c SomeBashCommand
\endfish
- or a *bash* shell can be opened with:
\fish
>/bin/bash
\endfish
This might be useful, when copying complicated *bash* commands from a website. Translating them into *fish* syntax can be avoided that way.
- or `bash` without a switch, opens a *bash* shell that you can use and `exit` afterwards.
<a href="#install"><-</a><hr>
\section shell What is a Shell
This section is about the basics of shells. *fish* is a shell and shells have a lot in common:
- <a href="#shell-standards">Shell Standards</a>: Why shells adjust to standards
- <a href="#man-page">Manual Pages</a>: Commands usually come with a standardized manual page
- <a href="#shell-syntax">Command Syntax</a>: Shell commands have a standard syntax
- <a href="#shell-programming">Commands versus Programs</a>: Commands differ from normal programs
- <a href="#shebang-line">Shebang Line</a>: How the shell knows the language of a script
\subsection shell-standards Shell Standards
A shell is an interface to the operating system that reads from the commandline of a terminal. A shell's task is to identify and interpret commands. The commands can come from different applications and can be written in different programming languages.
This can only work smoothly if shells adapt to some common standards. For shells there is the POSIX standard, see <a href="https://en.wikipedia.org/wiki/Command-line_interface#Command-line_interpreter">Command-line interpreters</a>. `fish` tries to satisfy the POSIX standard wherever it does not get into the way of its own design principles, see <a href="design.html">`fish` Design</a>.
\subsection man-page Manual Pages
There is a common standard on how to receive help on shell commands: applications provide a manual page to their commands that can be opened with the `man` command:
\fish
>man COMMAND
\endfish
This convention helps to make sure help can be found on commands no matter where they originate from. *fish*'s internal commands all come with a manual page.
\subsection shell-syntax Command Syntax
Shell commands also have some syntax in common, no matter where they originate from:
Commands consist of three parts:
\fish
COMMAND [OPTIONS] ARGUMENTS
\endfish
- <b>`COMMAND`</b>: the name of the executeable
- <b>`[OPTIONS]`</b>: options can change the behaviour of a command
- <b>`ARGUMENTS`</b>: input to the command
For external <b>commands</b> the executable is usually stored on file and the file path must be included in the variable `$PATH`, so that the file can be found: see <a href="#variables-special">Special Variables</a>.
<b>Options</b> come in two forms: a short name, that is a hyphen with a single letter; or a long name, consisting of two hyphens with words connected by hyphens. Example: `-h` and `--help` are common options for opening the manual page of a command. Options are a fixed set, described in the manual pages of the command, see <a href="#man-page">Manual Pages</a>
<b>Arguments</b> are the arbitrary input part of a command: often it is a file or directory name, sometimes it is a string or a list.
Example:
\fish
>echo -s Hallo World!
HalloWorld!
\endfish
- both `Hallo` and `World!` are arguments to the echo command
- `-s` is an option that suppresses spaces in the output of the command
\subsection shell-programming Commands versus Programs
Shell commands differ from other programs in the following way:
- they cannot return variables
Instead:
- They transform an <b>input stream</b> into an <b>output stream</b>. Both of these streams are usually the terminal, but they can be redirected.
- They return an <b>exit code</b>: this exit code is 0 when the command executes normally and between 1 and 255 otherwise.
This leads to another way of programming for shell commands: they can directly return variables, but they can pass on content to other commands in following ways:
<b>Commands can pass on content as a stream</b>: one command takes the output stream of another command as its input stream:
Example:
\fish
>ls -l | grep "my topic"
\endfish
- every line of the `ls` command is immediatelly passed on to the `grep` command and treated separately.
In this case the two commands execute in parallel. This is called piping, see <a href="#piping">Piping</a>.
<b>Commands can pass on all of their output stream as a chunk</b>:
Example:
\fish
>echo (ls a*)
\endfish
- the `echo` command takes the output of the `ls` command as a chunk and prints it to the terminal. The `echo` command waits for the `ls` command to finish, before it executes.
In this case one command waits for the other command to finish, before it executes. This is called command substitution, see <a href="#expand-command-substitution">Command Substitution</a>.
\subsection shebang-line Shebang Line
Since script for shell commands can be written in many different languages, they need to carry information about what interpreter is needed to execute them: For this they are expected to have a first line, the shebang line, which names an executable for this purpose:
Example:
A scripts written in `bash` it would need a first line like this:
```
#!/bin/bash
```
- this line tells the shell to execute the file with the *bash* interpreter, that is located at the path `/bin/bash`.
For a script, written in another language, just replace the interpreter `/bin/bash` with the language interpreter of that other language (for example `/bin/python` for a `python` script)
This line is only needed when scripts are executed by another interpreter, so for *fish* internal commands, that are executed by *fish* the shebang line is not necessary.
<a href="#shell"><-</a><hr>
\section syntax Syntax overview