How to run command or code in parallel in bash shell under Linux or Unix

How do I run commands in parallel in a bash shell script running under Linux or Unix-like operating system? How can I run multiple programs in parallel from a bash script?

You have various options to run programs or commands in parallel on a Linux or Unix-like systems:
=> Use GNU/parallel or xargs command.
=> Use wait built-in command with &.
=> Use xargs command.
How to run multiple programs in parallel from a bash script in linux / unix?
This page shows how to run commands or code in parallel in bash shell running on a Linux/Unix systems.


Putting jobs in background

The syntax is:
command &
command arg1 arg2 &
custom_function &

prog1 &
prog2 &

In above code sample, prog1, and prog2 would be started in the background, and the shell would wait until those are completed before starting the next program named progr3.


In this following example run sleep command in the background:
$ sleep 60 &
$ sleep 90 &
$ sleep 120 &

To displays status of jobs in the current shell session run jobs command as follows:
$ jobs
Sample outputs:

[1]   Running                 sleep 60 &
[2]-  Running                 sleep 90 &
[3]+  Running                 sleep 120 &

Let us write a simple bash shell script:

# Our custom function
  echo "Do something $1 times..."
  sleep 1
# For loop 5 times
for i in {1..5}
	cust_func $i & # Put a function in the background
## Put all cust_func in the background and bash 
## would wait until those are completed 
## before displaying all done message
echo "All done"

Let us say you have a text file as follows:
$ cat list.txt
Sample outputs:

To download all files in parallel using wget:

# Our custom function
  wget -q "$1"
while IFS= read -r url
        cust_func "$url" &
done < list.txt
echo "All files are downloaded."

GNU parallel examples to run command or code in parallel in bash shell

From the GNU project site:

GNU parallel is a shell tool for executing jobs in parallel using one or more computers. A job can be a single command or a small script that has to be run for each of the lines in the input. The typical input is a list of files, a list of hosts, a list of users, a list of URLs, or a list of tables.

The syntax is pretty simple:
parallel ::: prog1 prog2
For example, you can find all *.doc files and gzip (compress) it using the following syntax:
$ find . -type f -name '*.doc' | parallel gzip --best
$ find . -type f -name '*.doc.gz'

Install GNU parallel on Linux

Use the apt command/apt-get command on a Debian or Ubuntu Linux:
$ sudo apt install parallel
For a RHEL/CentOS Linux try, yum command:
$ sudo yum install parallel
If you are using a Fedora Linux, try dnf command:
$ sudo dnf install parallel


Our above wget example can be simplified using GNU parallel as follows:
$ cat list.txt | parallel -j 4 wget -q {}
$ parallel -j 4 wget -q {} < list.txt

See also
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

2 comments… add one
  • Haravikk May 5, 2017 @ 21:28

    Could really do with showing use of $! to get the PID as well IMO; very handy, especially for a bash script, when you want to be able to kill a long-running (or never-ending) process later, or wait for a specific process to end.

    Maybe it’s just me but I always felt it was good practice to store the PID from $! after every asynchronous call.

    For example:

    for i in {1..5}; do
            sleep $(($RANDOM % 60)) &
            pids+=( $! )
    for pid in "${pids[@]}"; do
            printf 'Waiting for %d...' "$pid"
            wait $pid
            echo 'done.'

    Handy for things like splitting off one process per core and such.

  • Sitaram May 9, 2017 @ 0:07

    I found GNU parallel to be way over-engineered, so I wrote “map”; see (and especially I guess). I must warn you it’s a bit dated; I use it every day but have not needed any changes for a long time.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.