10 Useful Chaining Operators in Linux with Practical Examples

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

32 Responses

  1. EBI says:

    GREAT ! APPRECIATED

  2. Naser bayat says:

    Just I want to thank all that share your experiences in the best way, it was so much useful for me.

  3. adren says:

    this article is wrong in many aspects:

    example #1 is not only bogus in many ways (you should never launch 2 commands in background that depend on each other, because the “apt-get upgrade” cannot work if the update is not finished, on top of that it is not recommended to launch in background a command that has output in the terminal)

    same logic applies with #2 (ping … &): there is no reason why one would ping a server in background (or at least do it with a quiet mode or redirecting standard output to /dev/null)

    example #3 is better, but I’m usually using the && operator (if update finishes without error, then upgrade should follow)

    as for #9, parenthesis is _NOT_ a _precedence_ operator, they are just used to run whatever commands inside in a subshell (hence, having precedence because of the priority over the rest, just like in mathematics)

    and of course, in #10 ‘\’ is not a concatenation but an escape characters (conCATenation is the ‘cat’ command, which should almost always be used with several arguments or wildcards, but shall not be used in the following form : “cat file | whatevercommand” -> see the YAUUoC syndrome)

    last warning:
    be very careful when using the extglob !(…) pattern (even more when used in a recurse deletion (rm -rf) command !!!)
    because it works perfectly when used alone (just like in the example given “rm -r !(*.html)”), but fails when one would like to use it with other arguments (so, big gotchas ahead / watch out!!!)

  4. nitpicker says:

    The example in #8 doesn’t even use the {} operator in any way, am I missing something?

  5. wildelk says:

    The example in #8 is not an example of “command combination”
    An example would be:

    [ -d bin ] || { echo Directory does not exist, creating directory now.; mkdir bin; } && echo Directory exists.

    Thanks for the article. I’ve been using Linux since 1997 and I manage 100+ linux servers in my companies datacenter. I still always find new stuff. The “command combination” was one of those.

  6. Jan Fikar says:

    There is a mistake in description of &&, should be the opposite:

    The AND Operator (&&) would execute the second command only, if the execution of first command *SUCCEEDS* , i.e., the exit status of the first command is *0*.

  7. anon says:

    The backslash \ character is not a ‘concatenation’ operator. It is a character escape operator. It prevents a following character from being interpreted as special, and instead causes it to be interpreted as itself. It’s use at the end of a line is to escape the normal meaning of the end of line character (execute command is the normal meaning) and instead to be interpreted as itself (whitespace).

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

Join Over 300K+ Linux Users
  1. 177,942
  2. 8,310
  3. 37,548

Are you subscribed?