-1

how to run multiple bash scripts in 1 bash command ?

i use command

bash script1.sh

how to make it run multiple commands in 1 command ?

for example

bash script1.sh bash script2.sh bash script3.sh bash script4.sh

Please help

Joe Cola
  • 113
  • 3
  • 1
    You shouldn't need to write `bash script1.sh` - put a shebang of `#!/usr/bin/env bash` as the first line of each script then call it as just `script1.sh` (and lose the `.sh` suffix - Unix commands, including those implemented as shell scripts, don't have/need suffixes stating the language they're implemented in). – Ed Morton Feb 18 '23 at 13:20
  • See [How do I run a shell script without using "sh" or "bash" commands?](https://stackoverflow.com/q/8779951/4154375). – pjh Feb 18 '23 at 13:44
  • Also see [Should I save my scripts with the .sh extension?](https://askubuntu.com/q/503127) and [Erlkonig: Commandname Extensions Considered Harmful](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/). – pjh Feb 18 '23 at 13:45

1 Answers1

0

If you want to run all bash script in //:

for i in {1..4}; do bash "script${i}.sh" & done

If you put the control operator & at the end of a command, e.g. command &, the shell executes the command in the background in a subshell. The shell does not wait for the command to finish, and the return status is 0. Pid of the last backgrounded command is available via the special variable $!


If instead you want to run sequentially, use

printf '%s\n' script{1..4}.sh | xargs -n1 bash

or

for i in {1..4}; do bash "script${i}.sh"; done
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223