10 Useful Chaining Operators in Linux with Practical Examples

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

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. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  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...

36 Responses

  1. anon says:

    In bash, to use rm -- !(file.txt) you may need to enable extglob:

    # type bash -O extglob
  2. Avinash says:

    Can anyone explain "[ -d bin ]" command, what is ''-d bin” and what "[ ]" does?

  3. firstname.lastname says:

    Something wrong in the explanation of #6 and #9.

    #6, it’s not the same as ‘if … else …’, because in command1 && command2 || command3, if command1 succeeds and command2 fails, command3 will be executed, this is not the logic with ‘if … else …’.

    #9, command1 && command2 || command3 && command4, if command1 fails, command3 will defintely be executed! You have mistakes in your statements.

  4. EBI says:


  5. Naser bayat says:

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

  6. 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!!!)

  7. nitpicker says:

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

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

  9. 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*.

  10. 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 *

This site uses Akismet to reduce spam. Learn how your comment data is processed.