Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

Compound commands

&pagelevel(4)&pagelevel
for identifier [ in word ... ]; do command_list; done

or

for identifier [ in wort ... ]
do command_list
done

The for command can be used to repeatedly execute command_list. Each time a for loop is executed, identifier is set to the next word taken from the word_list. If word_list is omitted, the command_list is executed once for each positional parameter ("$@") that is set. Execution ends when there are no more words in the word_list.



select identifier [ in word_list ]; do command_list; done

or

select identifier [ in word_list ]
do command_list
done

The select command can be used to repeatedly execute a given command_list under input control. select prints the set of words in word_list on standard output, each preceded by a number. The PS3 prompt is then printed, and a line is read from the standard input. The content of the line read from standard input is saved in the variable REPLY. If this line matches the number of one of the listed words, then the value of the parameter identifier is set to the word corresponding to this number. If the line is empty, the word_list is printed again, and identifier is set to the name of the shell script. If in word_list is omitted, then the positional parameters are used instead (as is done with for loops). The select loop is executed repeatedly until a break built-in or end-of-file is encountered.


Example

The following POSIX shell script can be used to selectively print information on each individual file in the current directory:

select file in `ls`
do
  if [ -z "$file" ]
  then
     echo "Select number"
  else
     ls -lsid $file
  fi
done



case word in 
[[(]pattern[|pattern]...)command_list;;]...
esac

The case command provides for conditional branching to a command list on the basis of a pattern. case executes the command_list associated with the first pattern that matches word. The form of the patterns is the same as that used for file name generation (see the section “File name generation”).

The optional left parenthesis preceding pattern must be specified if case commands are used within command substitutions with $(...). This produces paired parentheses constructs in $(...).



if command_list1
then command_list2
[elif command_list3
then command_list4]...
[else command_list5]
fi

The if command can be used to conditionally execute a number of different command lists. The first list following if (command_list1) is executed as the condition, and if it returns a zero exit status (true), the list following the first then is executed (command_list2). Otherwise, the list following elif (command_list3) is executed as the next condition, and if its value is zero, the list following the next then (command_list4) is executed. Failing that, the else list (command_list5) is executed. If no else list or then list is executed, the if command returns a zero exit status.



while command_list1
do command_list2
done
until command_list1
do command_list2
done

The while and until commands define loops with exit conditions. A while command repeatedly executes the condition command_list1, and if the exit status of the last command in the list is zero (i.e. true), it executes the do list in the body of the loop (command_list2); otherwise the loop terminates. If no commands in command_list2 (the do list) are executed, the while command returns a zero exit status.

until may be used in place of while to negate the loop termination test. The until command tests whether the exit condition is false (non-zero) and terminates the loop as soon as it returns an exit status of zero (true).



continue number

continue continues with the next iteration of the superordinate for, while, until or select loop. If number has been specified, then the next iteration is performed in the superordinate loop which corresponds to number. If number is equal to 0, processing continues beyond the outermost loop. continue 1 has the same effect as continue.



break number

break aborts the superordinate for, while, until or select loop. If number has been specified, then execution continues in the superordinate loop which corresponds to number. If number is equal to 0, then the outermost loop is exited. break 1 has the same effect as break.



return number

If return is called in a function then the command returns to the calling procedure. The return status is determined by number or by the last command executed. If return is called outside of a function or in a procedure executed with a leading dot, then it has the same effect as a call to exit.



(command_list)

Executes command_list in a separate environment.

If two consecutive left-hand parentheses ( are required to define nested commands then these must be separated by a space. Failure to do so may result in their being interpreted as an arithmetical expression (see Command substitution).



command_list;}

command_list is simply executed in the current POSIX shell.

The left brace ’{’ must be followed by a space!
Note that unlike the parentheses ( and ), the braces { and } are reserved words and must be typed at the beginning of a line or after a semicolon (;) in order to be recognized as such.



[[ expression ]]

Evaluates the conditional expression and returns a zero exit status if expression is true; otherwise, 1.

See Conditional expressions for a description of expression.



function identifier { command_list;}
identifier () { command_list;}

Defines a function named identifier. This name is used to call the function like a command. The body of the function consists of the command_list that is enclosed within curly braces { and }.

The left brace ’{’ must be followed by a space!

The braces {} are unnecessary if

  • the command list consists of a single for, case, if, while, until or select statement.

    Example

    function identifier case word in ...

    identifier () for identifier...

  • the command list is executed in a subshell, i.e. () instead of {}.

    Example

    function identifier (command_list)

    identifier () (command_list)



time pipeline

The commands in pipeline are executed and the elapsed time and the user and system time are reported on standard error.

The following reserved words are only recognized as the first word of a command and when not quoted:

if      then    elif    else    fi      case    esac    for
select  while   until   do      done    {       }       function
time    [[      ]]