- How do I run multiple background commands in bash in a single line?
- Execute combine multiple Linux commands in one line
- Running multiple commands in one line in shell
- 6 Answers 6
- How to Run Multiple Linux Commands in One Single Command
- 1. Using Semicolon (;) Operator to Run Multiple Linux commands
- 2. Using AND (&&) Operator to Run Multiple Linux Commands
- 3. Using (||) Operator to Run Several Linux Commands
How do I run multiple background commands in bash in a single line?
Exactly how do you want them to run? If you want them to be started in the background and run sequentially, you would do something like this:
If you want sleep 3 to run only if sleep 2 succeeds, then:
If, on the other hand, you would like them to run in parallel in the background, you can instead do this:
And the two techniques could be combined, such as:
Bash being bash, there’s often a multitude of different techniques to accomplish the same task, although sometimes with subtle differences between them.
What happens if we do sleep 2 && sleep 3 & ? I tried it in bash and it seems to work the same way (tried with echo).
@Wajahat, Did you try sleep 2 && echo «foo» && sleep 3 & echo «bar»» ? The && operator tells the shell to execute the command before it, and only if that command executes without any error the shell executes the subsequent command. This is essentially different from ; , because ; lets the subsequent command execute regardless of the exit status of the previous command unless it was a fatal error. The & tells the shell to execute all of the «connected» commands preceding it, in the background. «connected» here means the command is part of a pipe ( | ) or a logical operator.
Of these list operators, ‘&&’ and ‘||’ have equal precedence, followed by ‘;’ and ‘&’, which have equal precedence. in bash manual.
For some reason this is not working for me — i=0;while [ $i -le 10 ]; do echo i:$i;i=$[$i+1];sleep 2;done; & j=10;while [ $j -ge 0 ]; do echo j:$j;j=$[$j-1];sleep 2;done; & or (i=0;while [ $i -le 10 ]; do echo i:$i;i=$[$i+1];sleep 2;done; &) ; (j=10;while [ $j -ge 0 ]; do echo j:$j;j=$[$j-1];sleep 2;done; &) gives out -bash: syntax error near unexpected token `&’ error. Am i missing something?
You need to add some parens in your last version —
Note : When a list is used with & , its exit status is always zero. So, the two cases above do the same thing(The shell executes both sides in turn asynchronously).
to run multiple background command you need to add & end of each command. ex: (command1 &) && (command2 &) && (command3 &)
The answers above use parentheses. Bash also can use braces for a similar purpose:
Note that the braces are more picky about syntax—the space after < , the space before >, and the final semicolon are required. In some situations the braces are more efficient because they don’t fork a new subshell. In this case I don’t know if it makes a difference.
You can use the bash command substitution $(command) like this:
Note that stdin and stdout are still linked to the parent process and redirecting at least the stdout can be tricky. So alternatively you can chain the commands in a single line then pass the string to the bash command to spawn a new process which will handle the execution.
bash -c "command1 ; command2" &
This is especially useful in a bash script when you need to run multiple commands in background.
This two statements should be equivalent. A bash process is spawn in both cases to handle the command (chain of commands) and the & at the end detaches the execution.
This time you can add &>/dev/null before the & at the end of the command to redirect at least the stdout and avoid the output on the stdout of the parent process. Something like:
bash -c "command1 ; command2" &>/dev/null &
Execute combine multiple Linux commands in one line
If you want to execute each command only if the previous one succeeded, then combine them using the && operator:
cd /my_folder && rm *.jar && svn co path to repo && mvn compile package install
If one of the commands fails, then all other commands following it won’t be executed.
If you want to execute all commands regardless of whether the previous ones failed or not, separate them with semicolons:
cd /my_folder; rm *.jar; svn co path to repo; mvn compile package install
In your case, I think you want the first case where execution of the next command depends on the success of the previous one.
You can also put all commands in a script and execute that instead:
#! /bin/sh cd /my_folder \ && rm *.jar \ && svn co path to repo \ && mvn compile package install
The backslashes at the end of the line are there to prevent the shell from thinking that the next line is a new command; if you omit the backslashes, you would need to write the whole command in a single line.
A more convenient way compared to using backslashes and && everywhere is to instruct the shell to exit the script if any of the commands fail. You do that using the set built-in function with the -e argument. With that, you can write a script in a much more natural way:
#! /bin/sh set -e cd /my_folder rm *.jar svn co path to repo mvn compile package install
Save that to a file, for example myscript , and make it executable:
You can now execute that script like other programs on the machine. But if you don’t place it inside a directory listed in your PATH environment variable (for example /usr/local/bin , or on some Linux distributions ~/bin ), then you will need to specify the path to that script. If it’s in the current directory, you execute it with:
The commands in the script work the same way as the commands in the first example; the next command only executes if the previous one succeeded. For unconditional execution of all commands, simply don’t call set -e :
#! /bin/sh cd /my_folder rm *.jar svn co path to repo mvn compile package install
Running multiple commands in one line in shell
So, /templates/apple will be copied to /templates/used AND /templates/inuse and then after that I’d like to remove the original.
Is cp the best way to do this, followed by rm ? Or is there a better way?
I want to do it all in one line so I’m thinking it would look something like:
cp /templates/apple /templates/used | cp /templates/apple /templates/inuse | rm /templates/apple
Is this the correct syntax?
6 Answers 6
You are using | (pipe) to direct the output of a command into another command. What you are looking for is && operator to execute the next command only if the previous one succeeded:
cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple
cp /templates/apple /templates/used && mv /templates/apple /templates/inuse
To summarize (non-exhaustively) bash’s command operators/separators:
- | pipes (pipelines) the standard output ( stdout ) of one command into the standard input of another one. Note that stderr still goes into its default destination, whatever that happen to be.
- |& pipes both stdout and stderr of one command into the standard input of another one. Very useful, available in bash version 4 and above.
- && executes the right-hand command of && only if the previous one succeeded.
- || executes the right-hand command of || only it the previous one failed.
- ; executes the right-hand command of ; always regardless whether the previous command succeeded or failed. Unless set -e was previously invoked, which causes bash to fail on an error.
Why not cp to location 1, then mv to location 2. This takes care of «removing» the original.
And no, it’s not the correct syntax. | is used to «pipe» output from one program and turn it into input for the next program. What you want is ; , which seperates multiple commands.
cp file1 file2 ; cp file1 file3 ; rm file1
If you require that the individual commands MUST succeed before the next can be started, then you’d use && instead:
cp file1 file2 && cp file1 file3 && rm file1
That way, if either of the cp commands fails, the rm will not run.
How to Run Multiple Linux Commands in One Single Command
If you use Linux daily, you will know that the command line is the most powerful tool when you working with files, installing and configuring system software, and running them. It becomes even more efficient if you run two or more commands at once on the command line and save a good deal of time.
In this tutorial, we’ll see the different ways in which we can combine and execute multiple Linux commands efficiently in one single line.
; | cmd1; cmd2 | The “;” operator runs all commands regardless of whether the earlier ones are failed or not. |
&& | cmd1 && cmd2 | The “&&” operator carries out the second command only if the preceding command executes successfully. |
|| | cmd1 || cmd2 | The “||” operator executes the second command only if the precedent command returns an error. |
Let me explain to you in more detail how you can execute multiple commands in Linux in one go.
1. Using Semicolon (;) Operator to Run Multiple Linux commands
The semicolon (;) operator enables you to run one or more commands in succession, regardless of whether each earlier command succeeds or not. For example, run the following three commands on one line separated by semicolons (;) and hit enter.
This will show you a listing of the current directory ( ls ), show which directory you’re currently in ( pwd ), print the disk usage of files (du), and display your account login name ( whoami ) all at once.
2. Using AND (&&) Operator to Run Multiple Linux Commands
In some scenarios, you want to make sure that the second command only executes if the first command is executed successfully. For example, run the two commands separated by (&&) operator, which is two ampersands.
$ sudo apt update && sudo apt upgrade
Here the first command updates the package database lists for packages that need upgrading. If there are no errors, it will execute the second command that will upgrade all the packages to a newer version.
I highly recommend using the (&&) operator rather than using (;) semicolon operator most of the time. This assures that you don’t do anything terrible. For example, if you run the following command to change to a directory and then delete everything recursively in that directory, you could end up destroying your system if the directory change didn’t take place.
3. Using (||) Operator to Run Several Linux Commands
At times you might want to run a second command only if the first command returns an error. To do this, you need to use (||) operator. For example, you want to verify that if the MyFolder directory exists and create it if it doesn’t.
$ [ -d ~/MyFolder ] || mkdir ~/MyFolder
In the above example, the MyFolder directory does not exist, so the second command creates the directory.
Conclusion
In this article, we have learned the three useful ways in which we can combine and run multiple Linux commands in the command line productively.