in Linux

Useful chaining operators in Linux

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

Chaining operator is something which helps to run multiple commands at once like we execute a script and task completes automatically. Usually, people use Bash shell command line Chaining Operators (Linux operator) in shell scripting but also we can use these operators on shell prompt.

Ampersand Operator (&)

The function of ‘&‘ is to make the command run in background. Just type the command followed with a white space and ‘&‘.

  • Run one command in the background:

[[email protected] ~]# yum -y update &

  • Run two command in background:

[[email protected] ~]# yum -y update & yum -y upgrade &

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] ~]# yum -y update; apt-get upgrade; mkdir test; touch index.js

AND Operator (&&)

The AND Operator (&&) would execute the second command only, if the execution of first command succeeds.

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

[[email protected] ~]# ping -c3 && links

OR Operator (||)

The OR Operator (||) allow you to execute second command only if the execution of first command fails, if the first command is executed successfully, with an exit status ‘0‘? Obviously! Second command won’t execute.

[[email protected] ~]# mkdir test || links

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 ‘files‘ in your home directory and ‘cd‘ to it.

[[email protected] ~]# mkdir files
[[email protected] ~]# cd files

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

[[email protected] ~]# 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 ‘files‘.

[[email protected] ~]# 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] ~]# rm -r !(*.html)

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

[[email protected] ~]# ls

a.html b.html


A; B # Run A and then B, regardless of success of A
A && B # Run B if and only if A succeeded
A || B # Run B if and only if A failed
A & # Run A in background.

Written By:

Blog for everything about hosting, website, server and technical support

Add a Comment

Your email address will not be published.

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