Linux few commands in one line

How to combine multiple commands in terminal?

That was not my question. It is just that half a minute of googling would have given you the answer. Hence, I was wondering what you had already found, if anything.

What makes you think I downvoted your question? I will neglect your anger. As a side-note, the question «which one is better: using ; or && to execute multiple commands in one line» is a lot better, has more research and would likely have attracted a great many upvotes.

There is a more generic variant of this question available at askubuntu.com/questions/334994/…. It deal with exactly the question I mention in the previous comment.

Don’t worry Giri. The essence of the question is interesting. I took the effort of making a generic variant of it.

2 Answers 2

Yes, separate with a semi-colon like so:

Most lanugauges/shells use the semi-colon to signify the end of a command and to start new while evaluating from left to right.

Or as @RobieBasak recommends, use && instead of ; to guard against coding accidents.

It’s a good idea to use && instead of ; . This ensures that subsequent commands are only executed if previous commands have not failed. This avoids some awkward consequences. For example: cd /somewhere_else; rm -Rf * could do something disastrous if /somewhere_else doesn’t exist or you mis-spell it; cd /somewhere_else && rm -Rf * protects you from this.

The ‘&&’ solution is so good, it should be the first recommendation in this answer. Please edit your answer and promote the better answer to first place.

Источник

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.
Читайте также:  Ppd файл в linux

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.

Run Multiple Commands Using Semicolon Operator

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.

Источник

How to Run Multiple Linux Commands at Once in Terminal

Running two or more commands in one line can save you a good deal of time and help you become more efficient and productive in Linux.

Running two or more commands in one line can save you a good deal of time and help you become more efficient and productive in Linux. There are three ways you can run multiple commands in one line in Linux:

Operator Example Explanation
; Command 1 ; Command 2 Run command 1 first and then command 2
&& Command 1 && Command 2 Run command 2 only if command 1 ends sucessfully
|| Command 1 || Command 2 Run command 2 only if command 1 fails
Читайте также:  Ноутбук для установки линукс

Let me show you in detail how you can chain commands in Linux.

Using ; to run multiple Linux commands in one line

The simplest of them all is the semicolon (;). You just combine several commands that you want to run using ; in the following fashion:

Here, cmd1 will run first. Irrespective of whether cmd1 runs successfully or with error, cmd2 will run after it. And when cmd2 command finishes, cmd3 will run. Let’s take an example you can practice easily (if you want to).

mkdir new_dir; cd new_dir; pwd

Running Multiple Commands Linux

In the above command, you create a new directory named new_dir with mkdir command. Then you switch to this newly created directory using cd command. Lastly, you print your current location with pwd command.

Using && to run multiple Linux commands

Sometimes you want to ensure that the in the chain of Linux commands, the next command only runs when the previous command ends successfully. This is where the logical AND operator && comes into the picture:

If you use Ubuntu or Debian based distributions, you must have come across this command that utilizes && concept:

sudo apt update && sudo apt upgrade

Multiple Linux Commands One Line

Here, the first command (sudo apt update) refreshes the package database cache. If there is no error, it will then upgrade all the packages that have newer versions available. Let’s take the earlier example. If the new_dir already exists, mkdir command will return an error. The difference in the behavior of ; and && can be seen in the screenshot below: Did you see how commands separated by && stopped when the first command resulted in an error?

Using || to run several Linux commands at once

You can use the logical OR operator (||) to run a chain of commands but the next command only runs when the previous command ends in error. This is opposite to what you saw with &&.

Linux Chain Commands

If cmd1 fails, cmd2 runs. If cmd2 runs successfully, cmd3 won’t run. In the screenshot above, mkdir new_dir command fails because `new_dir already exists. Since this command fails, the next command cd new_dir is executed successfully. And now that this command has run successfully, the next command pwd won’t run.

Bonus Tip: Combine && and || operators

You may combine the operators to run two or more Linux commands. If you combine three commands with && and || , it will behave as the ternary operator in C/C++ ( condition ? expression_true ; expression_false).

[ -f file.txt ] && echo "File exists" || echo "File doesn't exist"

Combine Multiple Commands Linux

Run the above command before and after creating the file.txt file to see the difference: You can also use ; , && and || to run multiple commands in bash scripts as well.

Читайте также:  Linux find name exec

More such Linux terminal tips

Like copy-paste in Linux terminal, running multiple commands at once is also one of the many Linux command line tips for saving time. Though elementary, it is an essential concept any Linux terminal user should know. Here are a few more. And if you are absolutely new to Linux commands, I have created a series of tutorials that will help you. I hope you liked this terminal trick. Stay tuned for more Linux command tips and tools.

Источник

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 

Источник

Оцените статью
Adblock
detector