10 Useful Chaining Operators in Linux with Practical Examples

Chaining of Linux commands means, combining several commands and make them execute based upon the behaviour of operator used in between them. Chaining of commands in Linux, is something like you are writing short shell scripts at the shell itself, and executing them from the terminal directly. Chaining makes it possible to automate the process. Moreover, an unattended machine can function in a much systematic way with the help of chaining operators.

Chaining Operators in Linux

10 Chaining Operators in Linux

Read Also: How to Use Awk and Regular Expressions to Filter Text in Files

This Article aims at throwing light on frequently used command­-chaining operators, with short descriptions and corresponding examples which surely will increase your productivity and lets you write short and meaningful codes beside reducing system load, at times.

1. Ampersand Operator (&)

The function of ‘&‘ is to make the command run in background. Just type the command followed with a white space and ‘&‘. You can execute more than one command in the background, in a single go.

Run one command in the background:

[email protected]:~$ ping ­c5 www.tecmint.com &

Run two command in background, simultaneously:

[email protected]:/home/tecmint# apt-get update & apt-get upgrade &

2. semi-colon Operator (;)

The semi-colon operator makes it possible to run, several commands in a single go and the execution of command occurs sequentially.

[email protected]:/home/tecmint# apt-get update ; apt-get upgrade ; mkdir test

The above command combination will first execute update instruction, then upgrade instruction and finally will create a ‘test‘ directory under the current working directory.

3. AND Operator (&&)

The AND Operator (&&) would execute the second command only, if the execution of first command SUCCEEDS, i.e., the exit status of the first command is 0. This command is very useful in checking the execution status of last command.

For example, I want to visit website tecmint.com using links command, in terminal but before that I need to check if the host is live or not.

[email protected]:/home/tecmint# ping -c3 www.tecmint.com && links www.tecmint.com

4. OR Operator (||)

The OR Operator (||) is much like an ‘else‘ statement in programming. The above operator allow you to execute second command only if the execution of first command fails, i.e., the exit status of first command is ‘1‘.

For example, I want to execute ‘apt-get update‘ from non-root account and if the first command fails, then the second ‘links www.tecmint.com‘ command will execute.

[email protected]:~$ apt-get update || links tecmint.com

In the above command, since the user was not allowed to update system, it means that the exit status of first command is ‘1’ and hence the last command ‘links tecmint.com‘ gets executed.

What if the first command is executed successfully, with an exit status ‘0‘? Obviously! Second command won’t execute.

[email protected]:~$ mkdir test || links tecmint.com

Here, the user creates a folder ‘test‘ in his home directory, for which user is permitted. The command executed successfully giving an exit status ‘0‘ and hence the last part of the command is not executed.

5. NOT Operator (!)

The NOT Operator (!) is much like an ‘except‘ statement. This command will execute all except the condition provided. To understand this, create a directory ‘tecmint‘ in your home directory and ‘cd‘ to it.

[email protected]:~$ mkdir tecmint 
[email protected]:~$ cd tecmint

Next, create several types of files in the folder ‘tecmint‘.

[email protected]:~/tecmint$ touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html

See we’ve created all the new files within the folder ‘tecmint‘.

[email protected]:~/tecmint$ ls 

a.doc  a.html  a.pdf  a.xml  b.doc  b.html  b.pdf  b.xml

Now delete all the files except ‘html‘ file all at once, in a smart way.

[email protected]:~/tecmint$ rm -r !(*.html)

Just to verify, last execution. List all of the available files using ls command.

[email protected]:~/tecmint$ ls 

a.html  b.html

6. AND – OR operator (&& – ||)

The above operator is actually a combination of ‘AND‘ and ‘OR‘ Operator. It is much like an ‘if-else‘ statement.

For example, let’s do ping to tecmint.com, if success echo ‘Verified‘ else echo ‘Host Down‘.

[email protected]:~/tecmint$ ping -c3 www.tecmint.com && echo "Verified" || echo "Host Down"
Sample Output
PING www.tecmint.com (212.71.234.61) 56(84) bytes of data. 
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=1 ttl=55 time=216 ms 
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=2 ttl=55 time=224 ms 
64 bytes from www.tecmint.com (212.71.234.61): icmp_req=3 ttl=55 time=226 ms 

--- www.tecmint.com ping statistics --- 
3 packets transmitted, 3 received, 0% packet loss, time 2001ms 
rtt min/avg/max/mdev = 216.960/222.789/226.423/4.199 ms 
Verified

Now, disconnect your internet connection, and try same command again.

[email protected]:~/tecmint$ ping -c3 www.tecmint.com && echo "verified" || echo "Host Down"
Sample Output
ping: unknown host www.tecmint.com 
Host Down

7. PIPE Operator (|)

This PIPE operator is very useful where the output of first command acts as an input to the second command. For example, pipeline the output of ‘ls -l‘ to ‘less‘ and see the output of the command.

[email protected]:~$ ls -l | less

8. Command Combination Operator {}

Combine two or more commands, the second command depends upon the execution of the first command.

For example, check if a directory ‘bin‘ is available or not, and output corresponding output.

[email protected]:~$ [ -d bin ] || { echo Directory does not exist, creating directory now.; mkdir bin; } && echo Directory exists.

9. Precedence Operator ()

The Operator makes it possible to execute command in precedence order.

Command_x1 &&Command_x2 || Command_x3 && Command_x4.

In the above pseudo command, what if the Command_x1 fails? Neither of the Command_x2, Command_x3, Command_x4 would executed, for this we use Precedence Operator, as:

(Command_x1 &&Command_x2) || (Command_x3 && Command_x4)

In the above pseudo command, if Command_x1 fails, Command_x2 also fails but Still Command_x3 and Command_x4 executes depends upon exit status of Command_x3.

10. Concatenation Operator (\)

The Concatenation Operator (\) as the name specifies, is used to concatenate large commands over several lines in the shell. For example, The below command will open text file test(1).txt.

[email protected]:~/Downloads$ nano test\(1\).txt

That’s all for now. I am coming up with another interesting article very soon. Till then Stay tuned, healthy and connected to Tecmint. Don’t forget to give your Valuable feedback in our comment section.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

36 Responses

  1. Minhaj says:

    Awesome article. I use pipe and AND operators in class subjects and now i feel like introducing all of them to my class.
    Thank you.

  2. Christopher Dunn says:

    “`
    && and || are (were?) backwards. Try the following:

    false || echo hi
    true && echo hi

    false -> 1 (failure) and true -> 0 (success)
    “`

  3. daghan says:

    there are mistakes to be corrected:
    AND Operator (&&)
    if the execution of first command SUCCEEDS
    OR Operator (||)
    the exit status of first command is ””’0””’

  4. Urvesh says:

    Excellent !! keep it up..

  5. Good to know these operators. I only use very few of them on daily basis. Need to start using all of ’em so that I don’t forget their usage.

    Thanks for sharing.

  6. devnull says:

    The example in 8 uses the “test” operator. See man test or man [ (yes open bracket) for more info. Curlies (ie. parenthesis or braces) are used to disambiguate variables. For example, if I do MYUSER=’root’, then “${MYUSER}rules” is “root-isgreat”, but $MYUSERrules is undefined. You can also use curlies for precedence (ie. running several commands before doing something else).

  7. root says:

    These mistakes happens when you do copy paste :-)

  8. karthikeyan says:

    Example 1: Run two command in background, simultaneously:

    i don’t think the command “apt-get update & apt-get upgrade &” will run simultaneously because the first apt-get will create a lock and second apt-get will have to wait for the lock release. so in this particular example they will run sequentially rather than simultaneously.

  9. Solerman says:

    Example 8 uses one operator in the title and another in the script. I think the title is wrong

  10. Jack says:

    This article looks like it’s a slightly rehashed version of this one:

    http://www.linuxnix.com/2012/07/23-awesome-less-known-linuxunix-command-chaining-examples.html

    See example 24 on that page to find out what Example 8 above is missing.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.