Command chaining is a concept to execute two or more commands in one execution to increase.

productivity

Reduce system resource usage (In some cases)

Short and sweet codes.

These are supported by almost every shell we know.

Today we will learn how to use different command chaining operators available for us in an easy way.

Command chaining operators

	& --Sends process background (so we can run multiple process parallel)
	; --Run multiple commands in one run, sequentially.
	 --To type larger command in multiple lines
	&& --Logical AND operator
	|| --Logical OR operator
	! -NOT operator ( Thanks for mgd@interbaun.com for giving good examples)
	| -- PIPE operator
	{} --Command combination operator.
	() --Precedence operator

& – Runs a command in the background

This operator is useful to send a process/script/command to background, so that we can execute other commands in foreground to increase effective utilization of system resources and to speed up the script execution. This is also called as Child process creation or forking in other programming languages.

Example 1: Run commands in the background

	$ping -c1 google.com &

Example 2: Run more commands in the background in single line $

	ping -c1 google.com & scp root@1.1.1.10:/opt/* /opt &

Above commands are run in the background parallel independent of other commands. Like this, we can run many commands parallel.

; – semicolon operator

This operator Run multiple commands in one go, but in a sequential order. If we take three commands separated by semicolon, second command will run after first command completion, third command will run only after second command execution completes. One point we should know is that to run second command, it do not depend on first command exit status.

Example 3: Execute ls, pwd, whoami commands in one line sequentially one after the other.

	ls;pwd;whoami

Note: The number of commands you can run is infinity as we said earlier. By default there is no limit on how many commands you can run with ; operator. We have checked this with 500 commands executed in one line. The limit depends only on memory or ulimit’s settings.

Example 4: Run a bash shell script IF statement in one command

	if [ $? -eq 0 ]; then echo “success”; else echo “fail”; fi

Example 5: Run online FOR loop

for i in *;do echo “file is $i”;done

Example 6: Run online while loop.

	while [ $VAR1 -eq 10 ]; do echo “Var value is $VAR1”; done

Example 7: Run online until loop

	until [ $VAR1 -eq 10 ]; do echo “Var value is $VAR1”; done

This is a kind of shell scripting at terminal or one liner shell scripting or temporary shell scripting. This will help you not to create a file for small and dirty scripts. Just execute your script directly at terminal.

– Concatenation operator

This operator is useful to execute a command, which is too big and spread on multiple lines. This operator help spread this command to do multiple lines for execution.

Example8: Try to copy a file /var/ftp/pub/webadmin/debina/read from sysloggerserver1.linuxnix.com to /opt/oradba/debadmin/logic/abc folder on sysloggerserver2.persistentsystems.co.in and I am at surendrahome.beamtele.com.

	ssh abc@sysloggerserver1.linuxnix.com: 
	 /var/ftp/pub/webadmin/debian/read 
	 abc@sysloggerserver2.persistent.co.in
	:/opt/oradba/debadmin/logic/abc/

Actually this a big command, but we divided this to multiple lines to make it look good and understandable by users.

‘&&’ –Logical AND operator

This operator let us you execute second command, if the first command runs successfully i.e exit status is zero. This is very much useful to check the success status of first command. For example, I want to connect to abc.com before connecting I want to check if that machine is ping-able or not. This is a kind of “if statement” in Shell scripting.

Example 9: Connect to a machine only if it is able to ping otherwise do not execute connect command.

	ping -c1 abc.com && ssh abc@abc.com

Example 10: Assign a value to variable if it’s not set.

	[ -z $VAR1 ] && VAR1=10

Example 11: Check if a folder exists or not. If it’s not present, create it.

	[ ! -d /var/temp ] && mkdir /var/temp

Example 12: Create folder, if the folder creation is successful, then only change the directory.

	mkdir abc && cd abc

Example 13: Change to a folder, if this is success, then list the content.

	cd /var/ftp && ls

Like this, you can use your imagination to use this operator effectively to save valuable time.

|| –Logical OR operator

This operator let us you execute second command, if the first command fails i.e exit status is greater than zero of first command. This operation is equivalent to else statement. This is very much useful to check the failed status of first command. For example, I want to create a folder, but before that I want to check if folder exit’s or not. If it’s not exists, then create it.

Example 14: Create a folder if the folder does not exit’s.

	[ -d /var/temp ] || mkdir /var/temp

Example 15: Ping to a machine and let users know if it’s not reachable in a meaning full way.

	ping -c1 google.com &> /dev/null || echo “There is some problem with network, please connect your network admin”

&& and || operators combination

We can use && and || to simulate if-else statement within one line. If the first command executed successfully we can execute second command otherwise we will execute third command.

Example 16: ping google.com, and display useful information to user if it is ping-able or not.

	ping -c1 google.com && echo “That's good, able to ping google.com” || echo “That's bad unable to ping google.com”

Example 17: Check if a file exists or not. If it exists inform user it exists otherwise create it.

	[ -f /var/abc.txt ] && echo “file exists” || touch /var/abc.txt

Example 18: Check if my the previous command executed successfully or not pwd /home/surendra

	[ $? -eq 0 ] && echo “Yes, pwd command successfully executed” || echo “pwd command failed”

! -NOT operator (Negation operator)

Update: A follower (Flatcap) of this blog suggested below examples for this operator. This operator is very much handy when you want to delete or move all the files expect .txt files we can use this operator.

Example 19: Remove all the files, which are not a .doc file.

	ls | grep -v doc | xargs rm -rf or rm -rf *.{xls, txt, pdf}

The above rm command have some disadvantage, if we have great number of file extensions (Like 10s) we have to enter all of them and with above command we are using total four commands to accomplish our task. So at this point we should use ! operator for removing all the files expect .doc files as shown below.

	rm !(*.doc)

Note: If the above command do not work enable glob option with shopt command.

Example 20: Move all the files expect .doc files to /opt

	mv !(*.doc) /opt/

Example 21: Copy all the files expect pdf files to /opt/all

	cp !(*.pdf) /opt/all 

| — PIPE operator

This is a known operator for many people, this operator is used to send output of first command as an input to second command.

Example 22: Count no of files/folder located in a folder

	ls -l | wc -l

Example 23: Display all the partition names in the system.

	df -h | awk ‘{print $1}’

{ } –Command combination operator

This operator used to combine two or more commands to be executed depending on the the previous command. This can be explained with an example Suppose I want to check if a file exists or not. If it does not exists, we have to display error, then we have to create it. Let us do that without this {} operator

Example 24: Check if /opt/abc.txt exists or not?

	[ -f /opt/abc.txt ] || echo “The file does not exist”; touch /opt/abc.txt

The touch command in the above command never depends on first command exit status. In order to make it depend on first command we have to use { } braces as shown below.

	[ -f /opt/abc.txt ] || { echo “The file does not exist”; touch /opt/abc.txt; }

Note: Make sure that you give ; operator at the end of last command and space between two braces as shown in above examples. This is mandatory and without it you may get error.

Example 25: Ping to a machine, if we are able to ping display some useful info, then try to connect to it using ssh.

	ping -c www.linuxnix.com && {echo “That is ping-able, too good..!”; ssh root@www.linuxnix.com;}

() –Precedence operator

This operator is useful to execute command in precedence order, which command to execute first. For example, see below example

	command1 && command2 || command3 && command4

If you see command2 executes if the command1 executed successfully. But once command2 executed remaining commands command3 and command4 are not going to execute. In order to eliminate this, we have to use precedence operator. So below command sequence command3 and command4 will be executed if command1 or command2 failed.

Example26:

	( caommand1 && command2 ) || ( command3 && command4 )

Let us see what we did in a quick overview with below table.


Description

;

&&

||

|

&

Will second command execute if first command executed successfully?

yes

yes

No

yes

yes

Will second command execute if first command failed?

yes

No

Yes

Yes

Yes

Will first command exit status depends to execute second command?

No

Yes

Yes

No

No

Will commands execute Parallel?

No

no

no

no

yes

 

In this way we can save some valuable time. Please comment your thoughts on this.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.