The Bourne shell a pure interpreter for a highly interactive, complex
and powerful programming language with syntax based loosely on
ALGOL 68. The basic input (from a user or from a file) into the interpreter (sh)
is a sequence of pipelined commands.
Note. In these notes quotation marks ("....") are put around fixed strings
of characters and defined terms are set up as links or cross_references. The hash mark indicates "Any number of the following, inclding none".
- pipeline::= command optional(input_redirection) #("|" command) optional("|" command output_redirection).
- input_redirection::= "<" file | "<<"string.
- output_redirection::=( ">" | ">>" ) file.
These notes don't give rules running commands in the background:
- background_command::=command "&",
and the two pseudo-boolean operators "&&" and "||":
- and_then::=command #("&&" command ), the first command is executed and if
it terminates successfully so is the second command, and so on.
- or_else::=command #("||" command), Here the commands are tried one after another until one of them is successful.
It is not clear at this time what the relative priorities of "&&" and "||" vs "|"
are. It is advisable to use brackets: "(" .... ")" and "{" ...."}".
Commands
- command::=local_assignments command_name arguments,
- local_assignments::=#(assignment separator),
- arguments::=#(separator argument).
Normally,
- separator::=whitespace~EOLN #(whitespace~EOLN). This can be changed by
assigning a value to the correct shell variable.
- argument::=#(word | string | escaped_symbol ),
- word::=#(value_of_shell_variable | value_of_argument | #(char~separator)),
- value_of_shell_variable::=dollar identifier | shell_variable_expression.
- value_of_argument::= dollar number | argument_expression | dollar hash | dollar asterisrk | ...
There are several special variables/values:
$# = number of arguments/parameters.
$$ = process id of this process.
$? = exit status of last command.
There is a special feature in shell scripts that lets you easily and simply
test a variable or argument and do special things with it depending on whether
or not it is defined.
The most common form of these is
${ i :- w } = If i is unset or null use w else use ${i}.
- shell_variable_expression::= dollar left_brace identifier operator word right_brace,
- argument_expression::= dollar left_brace identifier operator word right_brace.
- operator::=argument_or_shell_variable_operator.
- argument_or_shell_variable_operator::= optional(colon) ( "-" | "=" | "?" | "+" ).
Scripts
A script is a sequence of commands and control structures separated by command
separators
- script::=(pipeline | built_in_structure) #(command_separator (pipeline | built_in_structure)),
- command_separator::=EOLN #whitespace | semicolon #whitspace.
- terminated_script::= script command_separator.
Built_in Shell Commands and Structures
- built_in_structure::=assignment | selection | loop | other_command | block | subshell,
- selection::=if_then_fi | case_esac,
- loop::= for_do_done | while_do_done | ... .
- other_command::=exit_statement | echo_statement | export_statement... .
- block::="{" script "}". Used to make a sequence of commands into one command.
- subshell::="(" script ")". A new shell is stated up to run the script.
- assignment::= shell_variable"="argument. No spaces outside quotes!
- export_statement::="export" shell_variable #(separator shell_variable).
A variable that has been assigned a value in this process can be
exported to processes that this process calls. The variable
and its value are copied into the enviironment of all programs
thatthis process runs. They are never returned. Thus local
assignments remain in the process that assigned them and its
sub-processes.
- echo_statement::="echo" arguments, Argument values are output separated by a single space and (usually) terminated by a EOLN.
- exit_statement::="exit" argument.
The argument of an exit statement is reurned as an exit staus. The
convention is that and exit 0 indicates success, but all other
values indicate an error.
Control Structures
- condition::=terminated_script.
Any sequence of commands can act as a condition. Successful completion
acts a true value. Notice that "&&" and "||" act as short-circuited
boolean operators - conjunction(and_then) and disjunction(or-else).
- if_then_fi::= "if" condition "then" terminated_script #(elif_part) optional("else" terminated_script ) "fi".
- elif_part::= "elif" condition "then" terminated_script.
- while_do_done::="while" condition "do" terminated_script "done".
- case_esac::="case" argument "in" command_separator case #(";;" case) command_separator "esac".
- case::= case_expression")" script.
- for_do_done::= "for" shell_variable optional("in" arguments) command_separator "do" terminated_script "done".
Variables and Arguments
A shell variable is identified by an identifier and its value by putting a
dollar sign in front of it, see value_of_variable.
Arguments are numbered inside a shell script and their values are indicated
by prefixing a dollar sign, see value_of_argument above.
Strings
- string::= double_quoted_string | single_quoted_string | reverse_quoted_string,
- escaped_symbol::=backslash char,
- double_quoted_string::= double_quote #( value_of_shell_variable | value_of_argument | #(char~double_quote) | escaped_symbol) double_quote,
- reverse_quoted_string::=backquote pipeline backquote,
- single_quoted_string::=quote #(char~quote | escaped_symbol) quote.