I want to run all scripts in a directory at the same time.

I know that I can get a list of all the scripts and execute the first one in a directory with `ls ./*.sh`, but I can't seem to get all of them to run.

I also tried brace expansion {./*.sh; } but that also ran only the first script.

After I figure out how to run all of the them I want to run all of them in the background.

I know that I can probably do this with a for loop, but I was hoping there is a simple one liner using globbing or brace expansion that will get the job done simply.

How can I do this?

for script in ./*.sh; do "$script" & done

Or to limit the number of concurrent invocations, with GNU xargs:

xargs -n1 -P5 -r0a <(printf '%s\0' ./*.sh) env

(beware it assumes script names don't contain = characters).

Or with zsh:

autoload zargs # best in ~/.zshrc
zargs -n1 -P5 ./*.sh -- command

(here at most 5 at a time).

Beware that if those scripts produce any output they could end-up being badly interleaved. GNU parallel addresses that by storing the output of each command in a separate temporary file and outputting them in order:

parallel -j0 exec ::: ./*.sh

(-j0 to run all of them in parallel, remove to limit to the number of CPU cores, or specify the number yourself).

  • exec is not needed for GNU Parallel. In theory it could be faster, but I have not been able to measure a significant difference. – Ole Tange Dec 12 '16 at 2:16
  • @OleTange, it depends on the shell. Some shells optimise out the fork for the last command by themselves (bash only when there's only one command), some don't. The gain of CPU time and other resource incurred by that saved fork() will also vary greatly depending on the OS kernel. To limit the overhead, best would be to avoid the shell in the first place :-b – Stéphane Chazelas Dec 12 '16 at 7:34
  • I know the theory, but can you give an actual reproducible example where the above with give a measurable difference if exec is left out? – Ole Tange Dec 13 '16 at 3:50
  • @OleTange, compare, after ulimit -u 300: PARALLEL_SHELL=dash parallel -j0 sleep ::: {10..130} with PARALLEL_SHELL=dash parallel -j0 exec sleep ::: {10..130}. Also compare max memory usage (and overall CPU time). (note that some versions of dash did optimise the last fork, try with csh or fish if your dash is one of those). – Stéphane Chazelas Dec 13 '16 at 8:05
  • 1
    @iruvar, sh would also work if all those scripts are sh scripts meant to be interpreted by that sh. By using env, we're executing the scripts themselves so the she-bang (#! /path/to/interpreter) if any will be honoured. We need env because if you don't provide a command, xargs calls echo instead. A downside of using env is that it would not work properly for scripts whose name contains =. – Stéphane Chazelas Dec 19 '16 at 12:05

you can execute all the scripts in a folder by passing the folder to the run-parts command :

run-parts /path/to/folder &

Run every shell script in background.

sh -x script1.sh & sh -x script2.sh & sh -x script3.sh & 
  • Run each script in background mode so that next command is run without waiting for current command to complete.

  • '&' makes the scripts run in background so that prompt does not wait for it to complete


You can run any executable file in background by adding & to the end of the calling. foe example:

./executbaleFile [args] &

But when you just do that if you close the session or exit the terminal it will send a SIGHUP to the process that you are running.

If you want to make the process keep working even if you close the terminal, you have to nohup command. for example:

nohup ./executbaleFile [args] &

this will let the process working even after you close the terminal.

Or use disown., it removes the job from the shell's job list, so when terminating the shell it will not send a SIGHUP to the process.


Run the scripts in the background :

./myscript.sh & ./myscript2.sh & ....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.