- Multiple commands on a single line in Linux [duplicate]
- How can I run multiple commands which have & in one command line?
- 2 Answers 2
- Lists
- Job Control
- 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
- Execute combine multiple Linux commands in one line
Multiple commands on a single line in Linux [duplicate]
First, if you want to run multiple commands in one line, separate them by a ; :
The && is the logical and operator. If you issue
cmd2 will only run if cmd1 succeeded. That’s important to mention (also see below).
The & is not a logical operator in this case, it tells bash to run cmd1 in background.
In your case, the commandline needs syntactically look like this:
killall vsftpd && /usr/sbin/vsftpd & echo "OK"
However, I guess you really meant this:
killall vsftpd ; /usr/sbin/vsftpd & echo "OK"
because otherwise you would not be able to start the process if it is not already running, since killall would return a non zero return code.
Even having this the code is quite fragile. I suggest to use your operating systems facilities to start vsftp as a daemon. I mean facilities like the command start-stop-daemon .
Thanks for pointing out the difference between && and ; . Seems in most cases ; is preferable above && .
You can encapsulate commands (or sequences of commands) in parentheses, like so:
# killall vsftpd && (/usr/sbin/vsftpd &) && echo "OK"
However, this doesn’t make much sense semantically as the && token means “run subsequent command if previous command successful” (i.e., return value of 0), and the single & puts the command preceding it into the background (continuing immediately with the next command), which always yields success.
In your case, there really isn’t any way to determine the success of running the vsftpd command when it is backgrounded, unless the executable offers command-line arguments running the thing as a daemon so you needn’t background it manually.
How can I run multiple commands which have & in one command line?
I’ve encountered a headache problem. I want to execute mulitple commands in background, so I want to start them in bash one by one. It’s easy to start one command in linux shell in background, just like this:
But if I want to run multiple commands in background, I tried the following command format, but failed:
Panther, it seems using () could help fix this issue much easier than using a script. Let’s try to make full use of the features provided by the shell itself firstly, instead of composing what we want by ourselves. But of course, using script also could fix this issue. Thanks anyway.
What if I want to run a ‘tail -F
2 Answers 2
If you want to run them sequentially:
If you want them to run parallel:
In bash you can also use this (space behind the < and the ; are mandatory):
() could help bash to distinguish the & and &&. So just use () to seperate the execution unit when we want to use the & and && togather.
This starts myCommand1 and sends it to the background as it’s followed by ampersand, then immediately starts myCommand2 and sends also this to the background, therefore releasing the shell again.
Lists
For better understanding you may substitute pipeline by command here.
A list is a sequence of one or more pipelines separated by one of the operators ;, &, &&, or ||, and optionally terminated by one of ;, &, or .
If a command is terminated by the control operator &, 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. Commands separated by a ; are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.
AND and OR lists are sequences of one or more pipelines separated by the && and || control operators, respectively.
Source: man bash
Let’s break that down into examples. You can build a list by combining commands and separating them with one of these: ; & && || :
command1 ; command2 # runs sequentially command1 && command2 # runs sequentially, runs command2 only if command1 succeeds command1 || command2 # runs sequentially, runs command2 only if command1 fails command1 & command2 # runs simultaneously
You can terminate lists with one of these: ; & .
Normally you execute a command or a list by pressing Enter , that equals . The semicolon ; serves the very same purpose especially in scripts. Ampersand & however starts the command(s) in a subshell in the background, immediately releasing the shell.
You can use round () or curly brackets <> to further group lists, the difference being that round brackets spawn a subshell and curly ones don’t. Curly brackets need a space after the first and a semicolon or a newline before the closing bracket. For example:
# if c1 succeeds start a shell in the background # and run c2 and c3 sequentially inside it c1 && ( c2 ; c3 ) & # run c1 and if it succeeds c2 sequentially as a group command # if c1 or c2 fail run c3 in the background < c1 && c2 ;>|| c3 &
This can get quite complicated, if you’re unsure use true and false to test whether the construction works as expected:
Job Control
The jobs command displays a list of the background jobs that are running or have recently been finished in the current shell. There are a number of keyboard shortcuts and commands for job control:
- Ctrl + Z types the suspend character that causes the process currently running in the foreground to be stopped, it is not terminated, but remains in the jobs list
- Ctrl + Y types the delayed suspend character that causes the process currently running in the foreground to be stopped when it attempts to read input from the terminal
- fg = % brings a process into the foreground starting it if necessary, you can specify the process as follows:
% # last process in the jobs list %1 # 1st process in the jobs list %abc # process beginning with the string “abc” %?abc # process containing the string “abc” anywhere
%& # last process in the jobs list %1& # 1st process in the jobs list %abc& # process beginning with the string “abc” %?abc& # process containing the string “abc” anywhere
wait %1 # 1st process in the jobs list
Imagine you started a lengthy process ( jobs reveals it’s number 3) and then realize you want the computer to be suspended when it finishes, plus echo a message if the process didn’t succeed:
wait %3 || echo failed ; systemctl suspend
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.
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