6 BASH SHELL COMMAND LINE CHAINING OPERATORS (LINUX OPERATOR) WITH EXAMPLES
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 is helps to run multiple commands at once like we execute a script and task completes automatically. Usually people uses 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) :
&& Operator (AND Operator)
OR Operator (||)
AND & OR Operator (&& and ||)
PIPE Operator (|)
Semicolon Operator (;)
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. Now let’s take a example :
Here as you can see below the first command completed successfully hence the second command df -h executed.
elinuxbook@ubuntu:~$ 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 let’s see if the second command executes or not.
elinuxbook@ubuntu:~$ 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 :
elinuxbook@ubuntu:~$ 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 quiet interesting and will give you a nice output if you use it properly. The combination of this 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.
elinuxbook@ubuntu:~$ 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.
elinuxbook@ubuntu:~$ 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.
Also Read :
4. PIPE Operator (|)
The Fourth Bash shell command line Chaining Operators (Linux operator) is PIPE Operator (|).
PIPE is a kind of operator 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 :
elinuxbook@ubuntu:~$ ps -ef | more
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 00:13 ? 00:00:04 /sbin/init auto noprompt
root 2 0 0 00:13 ? 00:00:00 [kthreadd]
root 4 2 0 00:13 ? 00:00:00 [kworker/0:0H]
root 6 2 0 00:13 ? 00:00:00 [mm_percpu_wq]
root 7 2 0 00:13 ? 00:00:10 [ksoftirqd/0]
root 8 2 0 00:13 ? 00:00:08 [rcu_sched]
root 9 2 0 00:13 ? 00:00:00 [rcu_bh]
root 10 2 0 00:13 ? 00:00:00 [migration/0]
root 170 2 0 00:13 ? 00:00:00 [ttm_swap]
root 177 2 0 00:13 ? 00:00:02 [kworker/0:1H]
root 213 2 0 00:13 ? 00:00:02 [jbd2/sda1-8]
root 214 2 0 00:13 ? 00:00:00 [ext4-rsv-conver]
root 246 1 0 00:13 ? 00:00:02 /lib/systemd/systemd-journald
root 270 1 0 00:13 ? 00:00:01 /lib/systemd/systemd-udevd
systemd+ 326 1 0 00:13 ? 00:00:00 /lib/systemd/systemd-timesyncd
root 765 1 0 00:13 ? 00:00:00 /usr/sbin/vmware-vmblock-fuse -o subtype=vmware-vmblock,default_permissions,allow_other /var/run/vmblock-fuse
–More–
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 :
elinuxbook@ubuntu:~$ 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 Operators (Linux operator) is Ampersand Operator (&).
Ampersand Operator is a kind of operator which executes given commands in background. You can use this operator to execute multiple command 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 elinuxbook.com with 50 packets. as you know that it will take some time to complete the 50 packaets. 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.
elinuxbook@ubuntu:~$ ping -c 50 localhost &
You can also execute multiple commands using Ampersand Operator. Refer the command below.
elinuxbook@ubuntu:~$ ping -c 5 localhost & df -h &
[1] 25962
[2] 25963
elinuxbook@ubuntu:~$ 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.
These are 6 Bash shell command line Chaining Operators (Linux operator). Your feedback on this article is valuable for us.
If you found this article useful then Like us, Share this post on your preferred Social media, Subscribe our Newsletter OR if you have something to say then feel free to comment on the comment box below.