6 Bash Shell Command Line Chaining Operators in Linux

Introduction

In this article, I will explain you 6 Bash shell command line Chaining Operators (Linux operator). Now let’s discuss what is chaining operator. 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.

Here we are going to discuss on below Bash shell command line Chaining Operators (Linux operator) :

  1. && Operator (AND Operator)
  2. OR Operator (||)
  3. AND & OR Operator (&& and ||)
  4. PIPE Operator (|)
  5. Semicolon Operator (;)
  6. Ampersand Operator (&)

Now let’s discuss each and every Operator one by one.

1. && Operator (AND Operator)

The syntax to use AND Operator is :

# command 1 && command 2

AND Operator will execute the second command only if the First command executes successfully. Now have a look at the syntax above. Here command 2 will execute only if the command 1 executes successfully. For example :

Here as you can see below the first command completed successfully hence the second command df -h executed.

$ ping -c 5 localhost && df -h
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.067 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.086 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.085 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.081 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.086 ms

--- localhost ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4100ms
rtt min/avg/max/mdev = 0.067/0.081/0.086/0.007 ms
Filesystem      Size  Used Avail Use% Mounted on
udev            966M     0  966M   0% /dev
tmpfs           199M   12M  187M   6% /run
/dev/sda1        18G  5.3G   12G  32% /
tmpfs           992M  212K  992M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           992M     0  992M   0% /sys/fs/cgroup
tmpfs           199M   60K  199M   1% /run/user/1000

Now let’s test the AND Operator in different way. Here I will fail the first command and see if the second command executes or not.

$ ping -c 5 local && df -h
ping: unknown host local

As you can see above the First command failed hence the second command not get executed.

2. OR Operator (||)

On our list the Second Bash shell command line Chaining Operators (Linux operator) is OR Operator. The syntax to use OR Operator is :

# command 1 || command 2

OR Operator is completely opposite of && Operator. OR Operator will execute the second command only if the First command Fails. Now let’s take a Example :

$ ping -c 5 local || df -h
ping: unknown host local
Filesystem      Size  Used Avail Use% Mounted on
udev            966M     0  966M   0% /dev
tmpfs           199M   12M  187M   6% /run
/dev/sda1        18G  5.3G   12G  32% /
tmpfs           992M  212K  992M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           992M     0  992M   0% /sys/fs/cgroup
tmpfs           199M   60K  199M   1% /run/user/1000

As you can see above the second command executed i.e. “df -h” as the first command get failed.

3. AND & OR Operator (&& and ||)

Combination of && Operator & OR Operator (||) is quite interesting and will give you a nice output if you use it properly. The combination of these 2 operators like if…else statement in programming. Let’s take an example so that you will get more Idea on this :

Example : 1

Here I tried to success the First command and after a successful execution of the first command, we will get a message Directory created successfully.

$ mkdir data && echo "Directory created successfully" || echo "Directory creation failed"
Directory created successfully

As you can see above our first command executed successfully hence we are able to get the required message.

Example : 2

Here I tried to Failed the First command and after Failure of the first command we will get a message User creation failed.

$ useradd helpdesk  && echo "User created successfully" || echo "User creation failed"
useradd: user 'helpdesk' already exists
User creation failed

As you can see above our first command failed hence we are able to get the required message.

4. PIPE Operator (|)

The Fourth Bash shell command line Chaining Operators (Linux operator) is PIPE Operator (|). PIPE is a kind of operator that can be used to display output of the first command by taking input to the second command. For example, you want to check all currently running system process using command ps -ef. but as the processes list is so long that it cannot be covered in one screen. In that case, you can use the Filter Operator with command more. Refer the command below :

$ ps -ef | more
  UID   PID  PPID   C STIME   TTY           TIME CMD
    0     1     0   0 19Oct18 ??        28:26.69 /sbin/launchd
    0    47     1   0 19Oct18 ??         0:39.18 /usr/sbin/syslogd
    0    48     1   0 19Oct18 ??         0:18.18 /usr/libexec/UserEventAgent (System)
    0    51     1   0 19Oct18 ??         0:15.91 /System/Library/PrivateFrameworks/Uninstall.framework/Resources/uninstalld
    0    52     1   0 19Oct18 ??         0:34.08 /usr/libexec/kextd
    0    53     1   0 19Oct18 ??         4:35.44 /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/FSEvents.framework/Versions/A/Support/fseventsd
   55    58     1   0 19Oct18 ??         0:02.25 /System/Library/CoreServices/appleeventsd --server
    0    59     1   0 19Oct18 ??        11:23.76 /usr/sbin/systemstats --daemon
    0    61     1   0 19Oct18 ??         0:40.65 /usr/libexec/configd
    0    62     1   0 19Oct18 ??         6:05.79 /System/Library/CoreServices/powerd.bundle/powerd
    0    63     1   0 19Oct18 ??         1:16.99 /Library/PrivilegedHelperTools/com.80pct.FreedomHelper
    0    67     1   0 19Oct18 ??         3:01.31 /usr/libexec/logd
...

5. Semicolon Operator (;)

Fifth Bash shell command line Chaining Operators (Linux operator) is Semicolon Operator (;). Semicolon Operator executes multiple commands at once sequentially as it is mentioned and provides output without depending on the success & failure of other commands like && Operator and OR Operator (||). Have a look at the example below :

$ ping -c 5 localhost ; ifconfig ens33 ; df -h
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.063 ms
64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.091 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.085 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.086 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.086 ms

--- localhost ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4091ms
rtt min/avg/max/mdev = 0.063/0.082/0.091/0.011 ms
ens33     Link encap:Ethernet  HWaddr 00:0c:29:ff:cd:2e  
          inet addr:192.168.43.185  Bcast:192.168.43.255  Mask:255.255.255.0
          inet6 addr: 2405:204:f017:75dd:65af:f027:85c2:88eb/64 Scope:Global
          inet6 addr: 2405:204:f017:75dd:f076:72b8:fd36:757f/64 Scope:Global
          inet6 addr: fe80::b396:d285:b5b3:81c3/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:146608 errors:0 dropped:0 overruns:0 frame:0
          TX packets:78275 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:188124508 (188.1 MB)  TX bytes:6912561 (6.9 MB)

Filesystem      Size  Used Avail Use% Mounted on
udev            966M     0  966M   0% /dev
tmpfs           199M   12M  187M   6% /run
/dev/sda1        18G  5.3G   12G  32% /
tmpfs           992M  212K  992M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           992M     0  992M   0% /sys/fs/cgroup
tmpfs           199M   60K  199M   1% /run/user/1000

6. Ampersand Operator (&)

Sixth Bash shell command line Chaining Operator (Linux operator) is Ampersand Operator (&). Ampersand Operator is a kind of operator which executes given commands in the background. You can use this operator to execute multiple commands at once.

The advantage of this operator is it doesn’t let you wait to use the shell prompt till the given command get completed. For example, you ping to www.thegeekdiary.com with 50 packets. as you know that it will take some time to complete the 50 packets. Without using Ampersand Operator you have to wait till the 50 packets get completed to run another command but by using this operator you can use the shell prompt and the current given command i.e. pinging will run on background and that is the beauty of Ampersand Operator.

$ ping -c 50 localhost &

You can also execute multiple commands using Ampersand Operator. Refer the command below.

$ ping -c 5 localhost & df -h &
[1] 25962
[2] 25963

$ PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.052 ms

Filesystem      Size  Used Avail Use% Mounted on
udev            966M     0  966M   0% /dev
tmpfs           199M   12M  187M   6% /run
/dev/sda1        18G  5.3G   12G  32% /
tmpfs           992M  212K  992M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           992M     0  992M   0% /sys/fs/cgroup
tmpfs           199M   56K  199M   1% /run/user/1000

64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.091 ms
64 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.070 ms
64 bytes from localhost (127.0.0.1): icmp_seq=4 ttl=64 time=0.091 ms
64 bytes from localhost (127.0.0.1): icmp_seq=5 ttl=64 time=0.117 ms

--- localhost ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4072ms
rtt min/avg/max/mdev = 0.052/0.084/0.117/0.022 ms

As you can see above the output of both commands coming simultaneously.

Related Post