Lexicon
[ comp.text.ASCII.html ]
- |- white_space::= SP | HT | ...
- |- doublequote::="\"",
- |- quote::="'",
- |- backquote::="`",
- |- backslash::="\\".
- |- semicolon::=";".
Syntax
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.
- pipeline::= command (input_redirection|) #("|" command) ("|" command output_redirction|).
- input_redirection::= "<" file | "<<"string.
- output_redirection::=( ">" | ">>" ) file.
These notes fon'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 "&&", "||", "|"
are.
Commands
- command::=local_assignments command_name arguments,
- local_assignments::=#(assignment separator),
- arguments::=#(separator argument).
Normally,
- separator::=whitespace~EOLN #(whitespace~EOLN).
- argument::=#(word | string | escaped_symbol ),
- word::=#(value_shell_variable| value_of_argument | #(char~separator)),
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.
- 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,
- 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) O("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 O("in" arguments) command_separator "do" terminated_script "done".
Variables and Arguments
A shell variable is identified by an identifier:
- shell_variable::=identifier,
- value_of_variable::= "$" variable| "${" identifier operator word "}",
- operator::= O(colon) ( "-" | "=" | "?" | "+" ).
The most common form of these is
- ${ i :- w } = If i is unset or null use w else use ${i}.
Arguments are numbered inside a shell script:
- script_argument::= digit,
- value_of_argument::="$"digit "${" digit operator word "}".
There are several special variables/values:
- value::=value_of_argument | value_of_variable | "$" ( "$" | "?" | "#" | "!" | ...).
- $# = number of arguments/parameters.
- $$ = process id of this process.
- $? = exit status of last command.
Strings
- string::= double_quoted_string |single_quoted_string|reverse_quoted_string,
- escaped_symbol::=backslash char,
- double_quoted_string::= double_quote #( value_of_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.