25+ Awesome Linux/Unix command chaining examples

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


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 [email protected] 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.

Example1: Run commands in the background

	$ping -c1 google.com &

Example2: Run more commands in the background in single line $

	ping -c1 google.com & scp [email protected]:/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.

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


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 ulimits settings.

Example4: Run a bash shell script IF statement in one command

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

Example5: Run online FOR loop

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

Example6: Run online while loop.

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

Example7: 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 [email protected]: 
 [email protected]

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 lets 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.

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

	ping -c1 abc.com && ssh [email protected]

Example10: Assign a value to variable if it's not set.

	[ -z $VAR1 ] && VAR1=10

Example11: Check if a folder exists or not. If it's not present, create it.

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

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

	mkdir abc && cd abc

Example13: 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 lets 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 exits or not. If it's not exists, then create it.

Example14: Create a folder if the folder does not exits.

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

Example15: 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.

Example16: 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”

Example17: 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

Example18: Check if my 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.

Example19: 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.

Example20: Move all the files expect .doc files to /opt

	mv !(*.doc) /opt/

Example21: 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.

Example22: Count no of files/folder located in a folder

	ls -l | wc -l

Example23: 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 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

Example24: 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.

Example25: 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 [email protected];}

() –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.


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

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








Will second command execute if first command executed successfully?






Will second command execute if first command failed?






Will first command exit status depends to execute second command?






Will commands execute Parallel?







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

Show your love by sharing this..!
Share on VKShare on StumbleUponShare on YummlyPrint this pageFlattr the authorShare on TumblrEmail this to someoneBuffer this pageShare on Google+Share on LinkedInShare on FacebookTweet about this on TwitterPin on PinterestShare on RedditDigg this
The following two tabs change content below.
My name is Surendra Kumar Anne. I hail from Vijayawada which is capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. I work with ICE systems, Sydney as Sr. Consultant. You can contact me at surendra (@) linuxnix dot com.
  • Gouthamk10

    good work,nice explanation

  • gouthamk10

    good work.nice expanation

  • Chaturvedi Kuldeep

    The title of this post was attractive but after visiting this page, nothing  looks like less known commands. These are all very common operators in Shell scripting or Shell navigation.

    • http://www.linuxnix.com Surendra

      I agree.. Updated the post title with “23 Awesome Linux/Unix command chaining examples” no hard feelings..!

  • No

    but why are we advocating the practice of sshing to nodes as root?

  • Alireza

    Great post! But they’re not less-known commands! I think every Linux Geek knows them!

  • David Dreggors

    One comment…
    In your examples above you say “Exampl4: Run a bash shell script IF loop in one command”.

    If is not a loop, it is a conditional statement. It defines separate sets of instructions (commands) to run based on conditional outcome of an expression (if $var < 10 then run commandA else run commandB). A loop repeats the same code until a certain criteria is met (while $var < 10 do commandA ).

    Nice article though, that will help lot's of people that are new to the command line.

  • http://www.linuxnix.com Surendra

    Thought some one may try this example so want to warn them before executing. I just updated the post and removed the note.


  • http://www.linuxnix.com Surendra

    True.. update the post, removed less known word :)

  • http://www.linuxnix.com Surendra

    Thanks for the hands-up.. Updated the post.

    • kishore

      executing two shell scripts in unix from sigle terminal

  • Yakkaty Chandu

    Good Article

  • Alec

    You should never ssh to a root user, just ask Aaron Barr, may want to deter that.

    You also don’t really need the username argument, unless you want to access a different account on the target.

  • Fake

    Good summary of those operators.

  • flatcap

    You wanted an example of the ! (not) operator:
    $ rm !(*.txt)
    Remove all the files that DON’T MATCH *.txt (i.e. text files are left behind).

  • http://www.linuxnix.com Surendra

    Thank you Flatcap. Updated the post..!

  • Pingback: Linux Caracas - Encadenando comandos, con ejemplos prácticos()

  • Pingback: Links 30/7/2012: Wine 1.5.9, Warsow 1.0 Released | Techrights()

  • CTW

    Very nice and well written.

    • http://www.linuxnix.com Surendra

      Thanks CTW.. please spread about “The Linux Juggernaut”

  • mgd

    Just a niggly…in your “! Not Operator” section you say:

    rm !(*.doc)

    Example19: Move all the files expect .doc files to /opt

    mv !(*.doc) /opt/

    Example20: Copy all the files expect pdf files to /opt/all

    cp !(*.pdf) /opt/all

    When, instead of “expect”, you should be using “except”.

    rm !(*.doc)Example19: Move all the files except .doc files to /optmv !(*.doc) /opt/Example20: Copy all the files except pdf files to /opt/all
    Great article, very helpful.

  • Narasimha Karumanchi

    Nice post Surendra.


  • harsh

    really a sweet collection..

  • Aveen

    Just love it!! more helpful for beginners like me.

  • Farhaaz

    Nice and simple explanation…

  • Nica MLG

    Very good, while not new to linux or CLI, I have not really gone deep on the subject, now I want to dig it and learn AMAP about it and, just perhaps, obtain a linux cert… I’ve started to read your post, as they’re well written (exxept the ‘except’ word ;), definetly pun intended :)) and explains the subject in a very “simple” (meaning they’re easy to understand!) way.

    I liked them, so far. :)

  • SS

    good work

  • Mike Hunt

    Thank you , nice selection and informative

  • http://gravatar.com/adampasz adampasz

    Yes! Very helpful summary. Ignore the haterz. :)

    • http://www.linuxnix.com Surendra Anne

      Thanks for complements Adampasz.

  • http://sharp-code.net Hussain Ali

    very Sweet tips thanx

  • Sabysachi dey

    Superb Article….Really informative and interesting!


  • revertex

    It's not everyday ! stumbleupon well written, easy to understand examples like these.

  • http://www.jntuexpress.com Lakshman

    Amaxing article I have been searching for these commands :) I am going to use them to amaze my friends 😛

  • jabagawee

    For the question "Will commands execute Parallel?", you mentioned that the pipe operator will not run commands in parallel. This is false, and you can check with the following command:

    (echo "hi" && sleep 10) | cat

    The cat command will pick up the text immediately. This is because stdout and stdin are streams.

    • http://www.linuxnix.com Surendra Anne

      The commands which you given echo and sleep will run in other shell(A subshell) which are not run in present shell. If you try to execute commands in present shell you can not run them parallel when using |. Hope this clears your question.