Note: This document is not complete and completed progressivly with the time passing and using them in the projects such as QuantFE
Bash is the shell, or command language interpreter, for the GNU operating system.
Shells may be used interactively or non-interactively. In interactive mode, they accept input typed from the keyboard. When executing non-interactively, shells execute commands read from a file.
Basic Shell Features
Shell Operation
graph LR
A[Read Input]
subgraph B [Tokens]
direction RL
B1(Words)
B2(Operators)
end
C[Commands]
subgraph D [Expanded Tokens]
direction RL
D1(Filenames)
D2(Commands)
D3(Arguments)
end
F[Redirections]
G[Command Execution]
H[Collect Exit Status]
A -->|Quoting|B -->|parsing|C
C -->|Shell Expansions| D
D3 --> F
D2 --> G
Quoting
Quoting is used to remove the special meaning of certain characters or words to the shell.
There are three quoting mechanisms: the escape character, single quotes, and double quotes.
%%{init: {'theme':'forest'}}%%
flowchart LR
A[Quoting Mechanisims] -->B[espcape character \] & C[single quotes ' '] & D["double #quot; #quot;"]
Shell Commands
Reserved Words
The following words are recognized as reserved when unquoted and the first word of a command (see below for exceptions):
if then elif else fi time
for in until while do done
case esac coproc select function
{ } [[ ]] !
in is recognized as a reserved word if it is the third word of a case or select command. in and do are recognized as reserved words if they are the third word in a for command.
Simple Commands
command arg1 arg2 ... argn
Pipelines
A pipeline is a sequence of one or more commands separated by one of the control operators ‘ |
’ or ‘ | &’. |
The format for a pipeline is
command1 | command2
command1 |& command2
[time [-p]] [!] command1 [ | or |& command2 ] …
The output of each command in the pipeline is connected via a pipe to the input of the next command. That is, each command reads the previous command’s output. This connection is performed before any redirections specified by command1.
If ‘ | &’ is used, command1’s standard error, in addition to its standard output, is connected to command2’s standard input through the pipe; it is shorthand for 2>&1 | . This implicit redirection of the standard error to the standard output is performed after any redirections specified by command1. |
The reserved word time
causes timing statistics to be printed for the pipeline once it finishes.
List of Commands
A list is a sequence of one or more pipelines separated by one of the operators ‘;’, ‘&’, ‘&&’, or ‘ |
’, and optionally terminated by one of ‘;’, ‘&’, or a newline . |
Of these list operators, ‘&&’ and ‘ | ’ have equal precedence, followed by ‘;’ and ‘&’, which have equal precedence. |
If a command is terminated by the control operator ‘&’, the shell executes the command asynchronously in a subshell. This is known as executing the command in the background, and these are referred to as asynchronous commands.
command1 && command2
command2 is executed if, and only if, command1 returns an exit status of zero (success).
An OR list has the form
command1 || command2
command2 is executed if, and only if, command1 returns a non-zero exit status.
Compound Commands
Looping Constructs
until:
until test-commands; do consequent-commands; done
whilte:
while test-commands; do consequent-commands; done
for:
for name [ [in [words ...] ] ; ] do commands; done
An alternate form of the for
command is also supported:
for (( expr1 ; expr2 ; expr3)) ; do commands ; done
Conditional Constructs
if:
if test-commands; then
consequent-commands;
[elif more-test-commands; then
more-consequents;]
[else alternate-consequents;]
fi
case:
case word in
[(] pattern [| pattern]…) command-list ;;]…
esac
echo -n "Enter the name of an animal"
read ANIMAL
echo -n "The $ANIMAL has "
case $ANIMAL in
horse | dog | cat ) echo -n "four";;
man | kangaroo ) echo -n "two";;
* ) echo -n "an unknown number of";;
esac
echo " legs."
If the ‘;;’ operator is used, no subsequent matches are attempted after the first pattern match. Using ‘;&’ in place of ‘;;’ causes execution to continue with the command-list associated with the next clause, if any. Using ‘;;&’ in place of ‘;;’ causes the shell to test the patterns in the next clause, if any, and execute any associated command-list on a successful match, continuing the case statement execution as if the pattern list had not matched.
select:
The select
construct allows the easy generation of menus. It has almost the same syntax as the for
command:
select name [in words ...]; do commands; done
select fname in *;
do
echo you picked $fname \($REPLY\)
break;
done
((…)):
(( expression ))
If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1.
[[…]]:
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression.
[[ $line =~ [[:space:]]*(a)?b ]]
That means values for line
like ‘aab’, ‘ aaaaaab’, ‘xaby’, and ‘ ab’ will all match, as will a line containing a ‘b’ anywhere in its value.
If you want to match ‘initial string’ at the start of a line, the following will work:
[[ $line =~ ^"initial string" ]]
but this will not:
[[ $line =~ "^initial string" ]]
because in the second example the ‘^’ is quoted and doesn’t have its usual special meaning.
Expressions may be combined using the following operators, listed in decreasing order of precedence:
-
( expression )
Returns the value of expression. This may be used to override the normal precedence of operators.
-
! expression
True if expression is false.
-
expression1 && expression2
True if both expression1 and expression2 are true.
-
expression1 || expression2
True if either expression1 or expression2 is true.
The &&
and ||
operators do not evaluate expression2 if the value of expression1 is sufficient to determine the return value of the entire conditional expression.