2

About the control operators & and ;, from POSIX 2013:

For asynchronous lists,

the format for running a command in the background is:

command1 & [command2 & ... ]

For sequential lists,

the format for executing commands sequentially shall be:

command1 [; command2] ...
  1. Is & unary or binary? Is ; unary or binary?

    & and ; look like binary, which operate the commands before and after them, and indicate that the execution order between the two commands is concurrent or sequential.

    & looks like unary, because every command in an asynchronous list is followed by a &.

    ; doesn't look like unary, because the last command in a sequential list doesn't need a following ;.

    Note: operators operates on operands. Depending on the number of operands, we can say an operator is unary, binary, .... Control operators, if I understand correctly, are operators on commands.

  2. In a mixed list of & and ;, what is the execution order between the component commands? What does each of & and ; mean to the component commands?

    For example, in Bash

    $ echo 1 ; echo 2 & echo 3; echo 4 & echo 5
    1
    [1] 11089
    3
    2
    [2] 11090
    5
    4
    [1]-  Done                    echo 2
    [2]+  Done                    echo 4
    
    $ echo 1 ; echo 2 & echo 3; echo 4 & echo 5 ; echo 6 &
    1
    [1] 11100
    3
    [2] 11101
    5
    [3] 11102
    6
    4
    2
    [1]   Done                    echo 2
    [2]-  Done                    echo 4
    [3]+  Done                    echo 6
    
4
  1. I wouldn't normally think of them as "operators". I mean, typically operators return values. & and ; do not. They instead end statements. But if you take them as operators, they operate on a single statement—so they'd be postfix unary operators, I suppose.

  2. They're started left-to-right. Note that in the case of &, it runs in the background asynchronously: echo 1 & echo 2 & echo 3 could print 1, 2, and 3 in any order (because even though they were started in order, the kernel may not schedule them such that they print in order). But echo 1; echo 2; echo 3 will always print them in numerical order. echo 1; echo 2 & echo 3 will always print 1 first, but 2 and 3 could be in either order.

  • " I mean, typically operators return values. & and ; do not". I think a control operator takes commands as inputs and output a command instead of a value. For example, in command1 &, the input to & is command1, and the output is a new command command1 &. – Tim Mar 22 '16 at 20:33
  • ; doesn't look like unary, because the last command in a sequential list doesn't need a following ;. Also ; and & have meanings that indicate the execution order between the commands before and after them, and thus look like binary. – Tim Mar 22 '16 at 20:37
  • 1
    @Tim, in many contexts ; and <newline> are synonymous. Read the POSIX shell grammar. – Wildcard Mar 22 '16 at 20:43
  • @Tim Well, you could say the final ; is the newline—you can generally change newlines to ; and vice versa. But you can also add a ; at the end; e.g., if it's a binary operator, what does ls; mean? OTOH, ls; ; is a syntax error (which would be consistent with unary) – derobert Mar 22 '16 at 20:43
  • 1
    @Tim if it did, then why wouldn't ls ; ; be valid? Shell doesn't generally permit empty commands—e.g., if…then…else…fi doesn't allow empty commands either (e.g., if you want an empty then block, you have to explicitly use the : no-op command) – derobert Mar 22 '16 at 20:47
2

To add to derobert's answer, and dissect the command you include in your question:

echo 1 ; echo 2 & echo 3; echo 4 & echo 5 ; echo 6 &

This means in pseudocode:

print 1 (and complete this step before continuing)

start a process in the background that will eventually print 2,
  and keep going as soon as the process has been *started*

print 3 (and complete this before continuing)

start a background process that will eventually print 4,
  and keep going as soon as it has started

print 5 (complete this before continuing)

start a background process that will print 6.

Also note that any command to the shell must be terminated (whether with & or ; or a newline or end of file or something else I'm not thinking of now), otherwise the shell won't know when to stop reading in more characters as part of that command and actually run the command.

0

Note that bash has built-in commands that look very similar to the general case of a command line. I'm going to skip over that aspect.

But in general a command line is a series of commands separated by special punctuation, not operators.

A command is the name of the executable (which may be searched for via the PATH environment variable.) That's the program that will be executed to perform the command.

After the command are white space separated strings that are passed to the command as strings. But there are special punctuation characters that must be escaped ( using a \' or by using quotation marks) if you intend them to be part of an argument.

The punctuation characters have effects.

The special punctuation characters like '<', '>' and its kin define standard Input output and error to the command

'|' to terminate one command and pipe its standard output to the next commands standard input

';' to execute the previous command and wait for its completion. i.e. run in the foreground.

'&' to execute the previous command, but continue on with the command line process without waiting for completion. i.e. run in the background.

So be aware the intent of these syntactical marks is to allow you to put this stuff together do things like

echo perl myperlprogram.pl \< mydata.txt | at 12:35

Which makes a string using echo that is another shell command itself, to execute a perl program, and send that string to the at command to run it as a batch job later. But note that the < is escaped, so it is passed as an argument to echo (which will send it to at as input), instead of setting standard input right away. When the batch system and executes the command string THEN it will set the standard input of the perl program.

There are elaborations dealing with built in commands and things like parenthesis that can be learned once the basics are understood

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.