How to run multiple Linux commands from one line

Running multiple Linux commands from a single bash prompt is not only efficient, it’s easy to do.

How to run multiple Linux commands from one line
Running multiple Linux commands from a single bash prompt is not only efficient, it’s easy to do.

For nearly every Linux admin, the command line eventually becomes second nature. After a while, you wind up going straight for the bash prompt instead of any given GUI. That might be a way off for you, and there’s nothing wrong with that. 

In the meantime, you need to keep practicing your Linux commands, and finding ways to make that task a bit more efficient. One route to that particular goal is learning some of the tricks that make issuing commands a bit more efficient. 

Take, for instance, the ability to run multiple commands from a single bash prompt. The best example of this is updating and upgrading Ubuntu Linux. From the bash prompt you can do this by first running the sudo apt-get update command, followed by the sudo apt-get upgrade -y command. 

SEE: Choosing your Windows 7 exit strategy: Four options (TechRepublic Premium)

But what if you could combine those two commands in one? That way you don’t have to sit around and wait for the first command before issuing the second. Fortunately, this is actually quite easy. There are two ways to make this happen. The first is using the semicolon like so: 

sudo apt-get update; sudo apt-get upgrade -y 

The only problem with that method is that the second command will run, even if the first command fails. There may be instances where you don’t want this to happen. In those cases, you’d run the command:

 sudo apt-get update && sudo apt-get upgrade -y

 Both versions of the command will run, but the second form will only run the second command if the first completes successfully. 

This is very important for when you’re doing things like installing software from source. And you can do this with more than two commands. Say you want to update, upgrade, and autoremove with apt. Do that with:

 sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get autoremove -y

And that’s how you can easily run multiple commands from a single bash prompt. 

Also see

linuxadminhero.jpg

Image: iStockphoto/Spectral-Design