• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

What is the difference between & (ampersand) and && (double ampersand) while executing simultaneous commands on Linux

by admin

To run more that one command simultaneously we can use the & (ampersand) special character. Another use of & is running the commands in the background. In that case though, you should use & once and the end of the command or script. For example:

# [some command or script] &

Using & (ampersand) to run commands simultaneously

In order to run 2 commands simultaneously use the & special character between the 2 or more commands. The syntax is:

# command1 & command2 & command3 ..

For example, if you want to run 3 commands ‘uname -a’, ‘pwd’ and ‘ls’ simultaneously, you can use:

# hostname & pwd & date

The output would be similar to:

# hostname & pwd & date
[1] 3253
[2] 3254
/root
geeklab
Sat Jan 18 05:46:07 UTC 2020
[1]-  Done                    hostname
[2]+  Done                    pwd

When using single &, even when a command fails the next commands is run.

Using && (double ampersand) to run commands simultaneously

In case if you want that the second job/command needs to wait for the first job/command to finish, use the && between the commands. So in that way if any error occurs while the sentence is being executed it will stop. For example:

# hostname && pwd && date

Sample output:

$ hostname && pwd && date
geeklab01
/root
Sat Jan 18 12:23:34 IST 2020

Lets try using a wrong command and see if the next commands are executed:

$ hostname && wrngcmd && date
geeklab01
-bash: wrngcmd: command not found

As shown above the last command “date” was not executed as the command before it was wrong.

Filed Under: Linux

Some more articles you might also be interested in …

  1. chsh Command Examples in Linux
  2. test: command not found
  3. CentOS / RHEL 6 : Install and Configure SNMPv3
  4. CentOS / RHEL 5 : How to disable device mapper multipath (dm-multipath)
  5. UNIX / Linux : How to change the niceness (priority) of a process
  6. How to Install GUI On CentOS/RHEL 8
  7. mv Command Examples in Linux
  8. locale-gen: command not found
  9. Understanding Linux multipath (dm-multipath)
  10. chcon: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • ncat Command Examples in Linux
  • ncat: command not found
  • nautilus Command Examples in Linux
  • namei: command not found

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright