Difference between revisions of "Linking Commands"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
 
Line 1: Line 1:
[[Category:Shell Pipes]][[Category:Commands]]
+
Under bash you can create a sequence of one or more commands separated by one of the following operators:
 +
{| style="cellpadding="20" cellspacing="0" border="1"  width="100%"
 +
! style="background:#ffdead;width:8%"| Operator
 +
! style="background:#ffdead;width:15%;"| Syntax
 +
! style="background:#ffdead;width:32%;"| Description
 +
! style="background:#ffdead;width:45%;"| Example
 +
|-
 +
| ;
 +
| command1; command2
 +
| Separates commands that are executed in sequence.
 +
| In this example, pwd is executed only after date command completes.<br/><code>date ; pwd</code><br/>&nbsp;
 +
|-
 +
|&
 +
| command arg &
 +
| The shell executes the command in the background in a subshell. The shell does not wait  for the command to finish, and the return status is 0. The & operator runs the command in background while freeing up your terminal for other work.
 +
| In this example, find command is executed in background while freeing up your shell prompt.<br/><code>find / -iname "*.pdf" >/tmp/output.txt &</code><br/>&nbsp;
 +
|-
 +
| &&
 +
| command1 && command2
 +
| command2 is executed if, and only if, command1 returns an exit status of zero i.e. command2 only runs if first command1 run successfully.
 +
| See [[Logical AND]] section for examples.
 +
|-
 +
| &#124;&#124;
 +
| command1 &#124; command2
 +
| command2 is executed if and only if command1 returns a non-zero exit status i.e. command2 only runs if first command fails.
 +
| See [[Logical OR]] section for examples.
 +
|}

Revision as of 22:21, 20 September 2009

Under bash you can create a sequence of one or more commands separated by one of the following operators:

Operator Syntax Description Example
; command1; command2 Separates commands that are executed in sequence. In this example, pwd is executed only after date command completes.
date ; pwd
 
& command arg & The shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. The & operator runs the command in background while freeing up your terminal for other work. In this example, find command is executed in background while freeing up your shell prompt.
find / -iname "*.pdf" >/tmp/output.txt &
 
&& command1 && command2 command2 is executed if, and only if, command1 returns an exit status of zero i.e. command2 only runs if first command1 run successfully. See Logical AND section for examples.
|| command1 | command2 command2 is executed if and only if command1 returns a non-zero exit status i.e. command2 only runs if first command fails. See Logical OR section for examples.