{"id":11287,"date":"2019-03-12T22:11:25","date_gmt":"2019-03-12T22:11:25","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11287"},"modified":"2019-03-12T22:11:25","modified_gmt":"2019-03-12T22:11:25","slug":"10-useful-chaining-operators-in-linux-with-practical-examples","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/12\/10-useful-chaining-operators-in-linux-with-practical-examples\/","title":{"rendered":"10 Useful Chaining Operators in Linux with Practical Examples"},"content":{"rendered":"<p>Chaining of Linux commands means, combining several commands and make them execute based upon the behaviour of operator used in between them. Chaining of commands in Linux, is something like you are writing\u00a0<a title=\"Write linux shell scripts\" href=\"https:\/\/www.tecmint.com\/category\/bash-shell\/\" target=\"_blank\" rel=\"noopener\">short shell scripts<\/a>\u00a0at the shell itself, and executing them from the terminal directly. Chaining makes it possible to automate the process. Moreover, an unattended machine can function in a much systematic way with the help of chaining operators.<\/p>\n<div id=\"attachment_5080\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/12\/Chaining-Operators-in-Linux.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5080\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2013\/12\/Chaining-Operators-in-Linux.png\" alt=\"Chaining Operators in Linux\" width=\"435\" height=\"321\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">10 Chaining Operators in Linux<\/p>\n<\/div>\n<p><b>Read Also:<\/b>\u00a0<a href=\"https:\/\/www.tecmint.com\/use-linux-awk-command-to-filter-text-string-in-files\/\" target=\"_blank\" rel=\"noopener\">How to Use Awk and Regular Expressions to Filter Text in Files<\/a><\/p>\n<p>This Article aims at throwing light on frequently used\u00a0<strong>command\u00ad-chaining operators<\/strong>, with short descriptions and corresponding examples which surely will increase your productivity and lets you write short and meaningful codes beside reducing system load, at times.<\/p>\n<h3>1. Ampersand Operator (&amp;)<\/h3>\n<p>The function of \u2018<strong>&amp;<\/strong>\u2018 is to make the command run in background. Just type the command followed with a white space and \u2018<strong>&amp;<\/strong>\u2018. You can execute more than one command in the background, in a single go.<\/p>\n<p>Run one command in the background:<\/p>\n<pre>tecmint@localhost:~$ ping \u00adc5 www.tecmint.com &amp;<\/pre>\n<p>Run two command in background, simultaneously:<\/p>\n<pre>root@localhost:\/home\/tecmint# apt-get update &amp; apt-get upgrade &amp;<\/pre>\n<h3>2. semi-colon Operator (;)<\/h3>\n<p>The semi-colon operator makes it possible to run, several commands in a single go and the execution of command occurs sequentially.<\/p>\n<pre>root@localhost:\/home\/tecmint# apt-get update ; apt-get upgrade ; mkdir test<\/pre>\n<p>The above command combination will first execute\u00a0<strong>update<\/strong>\u00a0instruction, then\u00a0<strong>upgrade<\/strong>\u00a0instruction and finally will create a \u2018<strong>test<\/strong>\u2018 directory under the current working directory.<\/p>\n<h3>3. AND Operator (&amp;&amp;)<\/h3>\n<p>The\u00a0<strong>AND Operator<\/strong>\u00a0(<strong>&amp;&amp;<\/strong>) would execute the second command only, if the execution of first command\u00a0<strong>SUCCEEDS<\/strong>, i.e., the exit status of the first command is\u00a0<strong>0<\/strong>. This command is very useful in checking the execution status of last command.<\/p>\n<p>For example, I want to visit website\u00a0<strong>tecmint.com<\/strong>\u00a0using\u00a0<a href=\"https:\/\/www.tecmint.com\/command-line-web-browsers\/\" target=\"_blank\" rel=\"noopener\">links command<\/a>, in terminal but before that I need to check if the host is\u00a0<strong>live<\/strong>\u00a0or\u00a0<strong>not<\/strong>.<\/p>\n<pre>root@localhost:\/home\/tecmint# ping -c3 www.tecmint.com &amp;&amp; links www.tecmint.com<\/pre>\n<h3>4. OR Operator (||)<\/h3>\n<p>The\u00a0<strong>OR Operator<\/strong>\u00a0(<strong>||<\/strong>) is much like an \u2018<strong>else<\/strong>\u2018 statement in programming. The above operator allow you to execute second command only if the execution of first command fails, i.e., the exit status of first command is \u2018<strong>1<\/strong>\u2018.<\/p>\n<p>For example, I want to execute \u2018<strong>apt-get update<\/strong>\u2018 from non-root account and if the first command fails, then the second\u00a0<strong>\u2018links www.tecmint.com<\/strong>\u2018 command will execute.<\/p>\n<pre>tecmint@localhost:~$ apt-get update || links tecmint.com<\/pre>\n<p>In the above command, since the\u00a0<strong>user<\/strong>\u00a0was not allowed to\u00a0<strong>update<\/strong>\u00a0system, it means that the exit status of first command is\u00a0<strong>\u20181\u2019<\/strong>\u00a0and hence the last command \u2018<strong>links tecmint.com<\/strong>\u2018 gets executed.<\/p>\n<p>What if the first command is executed successfully, with an exit status \u2018<strong>0<\/strong>\u2018? Obviously! Second command won\u2019t execute.<\/p>\n<pre>tecmint@localhost:~$ mkdir test || links tecmint.com<\/pre>\n<p>Here, the user creates a folder \u2018<strong>test<\/strong>\u2018 in his home directory, for which user is permitted. The command executed successfully giving an exit status \u2018<strong>0<\/strong>\u2018 and hence the last part of the command is not executed.<\/p>\n<h3>5. NOT Operator (!)<\/h3>\n<p>The\u00a0<strong>NOT Operator<\/strong>\u00a0(<strong>!<\/strong>) is much like an \u2018<strong>except<\/strong>\u2018 statement. This command will execute all except the condition provided. To understand this, create a directory \u2018<strong>tecmint<\/strong>\u2018 in your home directory and \u2018<strong>cd<\/strong>\u2018 to it.<\/p>\n<pre>tecmint@localhost:~$ mkdir tecmint \r\ntecmint@localhost:~$ cd tecmint<\/pre>\n<p>Next, create several types of files in the folder \u2018<strong>tecmint<\/strong>\u2018.<\/p>\n<pre>tecmint@localhost:~\/tecmint$ touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html<\/pre>\n<p>See we\u2019ve created all the new files within the folder \u2018<strong>tecmint<\/strong>\u2018.<\/p>\n<pre>tecmint@localhost:~\/tecmint$ ls \r\n\r\na.doc  a.html  a.pdf  a.xml  b.doc  b.html  b.pdf  b.xml<\/pre>\n<p>Now delete all the files except \u2018<strong>html<\/strong>\u2018 file all at once, in a smart way.<\/p>\n<pre>tecmint@localhost:~\/tecmint$ rm -r !(*.html)<\/pre>\n<p>Just to verify, last execution. List all of the available files using\u00a0<a href=\"https:\/\/www.tecmint.com\/15-basic-ls-command-examples-in-linux\/\" target=\"_blank\" rel=\"noopener\">ls command<\/a>.<\/p>\n<pre>tecmint@localhost:~\/tecmint$ ls a.html  b.html<\/pre>\n<h3>6. AND \u2013 OR operator (&amp;&amp; \u2013 ||)<\/h3>\n<p>The above operator is actually a combination of \u2018<strong>AND<\/strong>\u2018 and \u2018<strong>OR<\/strong>\u2018 Operator. It is much like an \u2018<strong>if-else<\/strong>\u2018 statement.<\/p>\n<p>For example, let\u2019s do ping to\u00a0<strong>tecmint.com<\/strong>, if success echo \u2018<strong>Verified<\/strong>\u2018 else echo \u2018<strong>Host Down<\/strong>\u2018.<\/p>\n<pre>tecmint@localhost:~\/tecmint$ ping -c3 www.tecmint.com &amp;&amp; echo \"Verified\" || echo \"Host Down\"<\/pre>\n<h5>Sample Output<\/h5>\n<pre>PING www.tecmint.com (212.71.234.61) 56(84) bytes of data. \r\n64 bytes from www.tecmint.com (212.71.234.61): icmp_req=1 ttl=55 time=216 ms \r\n64 bytes from www.tecmint.com (212.71.234.61): icmp_req=2 ttl=55 time=224 ms \r\n64 bytes from www.tecmint.com (212.71.234.61): icmp_req=3 ttl=55 time=226 ms \r\n\r\n--- www.tecmint.com ping statistics --- \r\n3 packets transmitted, 3 received, 0% packet loss, time 2001ms \r\nrtt min\/avg\/max\/mdev = 216.960\/222.789\/226.423\/4.199 ms \r\n<strong>Verified<\/strong><\/pre>\n<p>Now, disconnect your internet connection, and try same command again.<\/p>\n<pre>tecmint@localhost:~\/tecmint$ ping -c3 www.tecmint.com &amp;&amp; echo \"verified\" || echo \"Host Down\"<\/pre>\n<h5>Sample Output<\/h5>\n<pre>ping: unknown host www.tecmint.com \r\n<strong>Host Down<\/strong><\/pre>\n<h3>7. PIPE Operator (|)<\/h3>\n<p>This\u00a0<strong>PIPE<\/strong>\u00a0operator is very useful where the output of first command acts as an input to the second command. For example, pipeline the output of \u2018<strong>ls -l<\/strong>\u2018 to \u2018<strong>less<\/strong>\u2018 and see the output of the command.<\/p>\n<pre>tecmint@localhost:~$ ls -l | less<\/pre>\n<h3>8. Command Combination Operator {}<\/h3>\n<p>Combine two or more commands, the second command depends upon the execution of the first command.<\/p>\n<p>For example, check if a directory \u2018<strong>bin<\/strong>\u2018 is available or not, and output corresponding output.<\/p>\n<pre>tecmint@localhost:~$ [ -d bin ] || { echo Directory does not exist, creating directory now.; mkdir bin; } &amp;&amp; echo Directory exists.\r\n<\/pre>\n<h3>9. Precedence Operator ()<\/h3>\n<p>The Operator makes it possible to execute command in precedence order.<\/p>\n<pre>Command_x1 &amp;&amp;Command_x2 || Command_x3 &amp;&amp; Command_x4.<\/pre>\n<p>In the above pseudo command, what if the\u00a0<strong>Command_x1<\/strong>\u00a0fails? Neither of the\u00a0<strong>Command_x2<\/strong>,\u00a0<strong>Command_x3<\/strong>,\u00a0<strong>Command_x4<\/strong>\u00a0would executed, for this we use\u00a0<strong>Precedence Operator<\/strong>, as:<\/p>\n<pre>(Command_x1 &amp;&amp;Command_x2) || (Command_x3 &amp;&amp; Command_x4)<\/pre>\n<p>In the above pseudo command, if\u00a0<strong>Command_x1<\/strong>\u00a0fails,\u00a0<strong>Command_x2<\/strong>\u00a0also fails but Still\u00a0<strong>Command_x3<\/strong>\u00a0and\u00a0<strong>Command_x4<\/strong>\u00a0executes depends upon exit status of\u00a0<strong>Command_x3<\/strong>.<\/p>\n<h3>10. Concatenation Operator (\\)<\/h3>\n<p>The\u00a0<strong>Concatenation Operator<\/strong>\u00a0(<strong>\\<\/strong>) as the name specifies, is used to concatenate large commands over several lines in the shell. For example, The below command will open text file\u00a0<strong>test(1).txt<\/strong>.<\/p>\n<pre>tecmint@localhost:~\/Downloads$ nano test\\(1\\).txt<\/pre>\n<p><a href=\"https:\/\/www.tecmint.com\/chaining-operators-in-linux-with-practical-examples\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Chaining of Linux commands means, combining several commands and make them execute based upon the behaviour of operator used in between them. Chaining of commands in Linux, is something like you are writing\u00a0short shell scripts\u00a0at the shell itself, and executing them from the terminal directly. Chaining makes it possible to automate the process. Moreover, an &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/12\/10-useful-chaining-operators-in-linux-with-practical-examples\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;10 Useful Chaining Operators in Linux with Practical Examples&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11287","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11287"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11287\/revisions"}],"predecessor-version":[{"id":11288,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11287\/revisions\/11288"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}