[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] / [Samples] / unix.syntax
[Index] [Contents] [Source Text] [About] [Notation] [Copyright] [Comment/Contact] [Search ]
Tue Sep 18 15:27:32 PDT 2007

Contents


    The UNIX Bourne Shell

      Notation Used in Syntax

      Definitions look like this
       	defined_term::=the_possible_forms_of_the_term.
      Defined_terms are linked to there definitions.

      The vertical bar (|) is read "or else" and indicates a choice of two forms. Symbols in the language are put in double_quotation marks.

      Three special notations:

    1. optional(X)::= X | empty.
    2. empty::=, -- nothing.
    3. #(X)::=any number of X's including none.
    4. #(X) = empty | X #(X).

      Lexicon

      [ comp.text.ASCII.html ]
    5. |- white_space::= ASCII.SP | ASCII.HT | ...
    6. |- doublequote::="\"",
    7. |- quote::="'",
    8. |- backquote::="`",
    9. |- backslash::="\\".
    10. |- semicolon::=";".
    11. |- colon::=":".
    12. |- dollar::="$".
    13. |- left_brace::="{".
    14. |- right_brace::="}".
    15. |- EOLN::=End of line.

      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.

        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".

      1. pipeline::= command optional(input_redirection) #("|" command) optional("|" command output_redirection).

      2. input_redirection::= "<" file | "<<"string.
      3. output_redirection::=( ">" | ">>" ) file.

        These notes don't give rules running commands in the background:

      4. background_command::=command "&",

        and the two pseudo-boolean operators "&&" and "||":

      5. and_then::=command #("&&" command ), the first command is executed and if it terminates successfully so is the second command, and so on.

      6. 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

      7. command::=local_assignments command_name arguments,
      8. local_assignments::=#(assignment separator),
      9. arguments::=#(separator argument). Normally,
      10. separator::=whitespace~EOLN #(whitespace~EOLN). This can be changed by assigning a value to the correct shell variable.

      11. argument::=#(word | string | escaped_symbol ),
      12. word::=#(value_of_shell_variable | value_of_argument | #(char~separator)),

      13. value_of_shell_variable::=dollar identifier | shell_variable_expression.
      14. 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}.

      15. shell_variable_expression::= dollar left_brace identifier operator word right_brace,
      16. argument_expression::= dollar left_brace identifier operator word right_brace.
      17. operator::=argument_or_shell_variable_operator.
      18. argument_or_shell_variable_operator::= optional(colon) ( "-" | "=" | "?" | "+" ).

        Scripts

        A script is a sequence of commands and control structures separated by command separators

      19. script::=(pipeline | built_in_structure) #(command_separator (pipeline | built_in_structure)),
      20. command_separator::=EOLN #whitespace | semicolon #whitspace.
      21. terminated_script::= script command_separator.

        Built_in Shell Commands and Structures

      22. built_in_structure::=assignment | selection | loop | other_command | block | subshell,
      23. selection::=if_then_fi | case_esac,
      24. loop::= for_do_done | while_do_done | ... .

      25. other_command::=exit_statement | echo_statement | export_statement... .
      26. block::="{" script "}". Used to make a sequence of commands into one command.
      27. subshell::="(" script ")". A new shell is stated up to run the script.
      28. assignment::= shell_variable"="argument. No spaces outside quotes!
      29. 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.

      30. echo_statement::="echo" arguments, Argument values are output separated by a single space and (usually) terminated by a EOLN.

      31. 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

      32. 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).

      33. if_then_fi::= "if" condition "then" terminated_script #(elif_part) optional("else" terminated_script ) "fi".

      34. elif_part::= "elif" condition "then" terminated_script.
      35. while_do_done::="while" condition "do" terminated_script "done".

      36. case_esac::="case" argument "in" command_separator case #(";;" case) command_separator "esac".
      37. case::= case_expression")" script.

      38. 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

      39. string::= double_quoted_string | single_quoted_string | reverse_quoted_string,

      40. escaped_symbol::=backslash char,

      41. double_quoted_string::= double_quote #( value_of_shell_variable | value_of_argument | #(char~double_quote) | escaped_symbol) double_quote,

      42. reverse_quoted_string::=backquote pipeline backquote,

      43. single_quoted_string::=quote #(char~quote | escaped_symbol) quote.

      . . . . . . . . . ( end of section Syntax) <<Contents | End>>

    . . . . . . . . . ( end of section The UNIX Bourne Shell) <<Contents | End>>

End