How To: String Commands Together In The Terminal

Today’s article is about how you can string commands together in the terminal. You can do so in a few different ways, for a few different reasons. This article will explain some of those to you, hopefully making your life easier.

There are a number of reasons why you’d want to string commands together. I find it easier to just run certain commands together. I also find it easier for some aliases. (Speaking of which, I really need to do an article about aliases!)

For example, you might want to run change to the Foo directory and then list the files inside it. Well, one way to write that command would be cd Foo && ls -la. In that case, you’d change the directory to Foo and then you’d list all the files within that directory. As I said, there are a number of ways to do this and a number of reasons why.

This article won’t be all that long – or even all that difficult. But, this is a pretty basic skill you’d want to have if you’re working in the terminal. It’s one of those tools you’ll (maybe) find yourself using more often once you’re used to it. I, for one, use it with some frequency. You can string commands together and then go make coffee!

String Commands Together:

Obviously, you’re going to need an open terminal. If you don’t know how to open the terminal, you can do so with your keyboard – just press CTRL + ALT + T and your default terminal should open.

With your terminal open, you’ll want one of the following operators:

; run the next command no matter what.
&& run the next command only if the first one succeeds.
|| run the next command only if the first one failed to run.

The ; operator can be used something like this:

cd Downloads ; touch Foo

Them the && operator can be used something like this:

sudo apt update && sudo apt upgrade -y

Finally, the || operator can be used something like this:

cd Foo || mkdir Foo

You could string them along something like:

cd Foo || mkdir Foo && cd Foo ; ls -la

See? It’s not all that complicated. You can also swap the ; operator for a single ampersand. It’ll do the same thing, run the following command regardless of the results of the first. So, cd Downloads ; touch foo and cd Downloads & touch foo are functionally the same.

Closure:

Yup, another article – said and done. This one is, as promised, not all that difficult or even all that long. Still, I hope you’ve learned something and know now how to string commands together in the terminal. It’s a pretty useful skill to have. Feel free to leave a comment explaining how you already use these operators to string commands together.

Thanks for reading! If you want to help, or if the site has helped you, you can donate, register to help, write an article, or buy inexpensive hosting to start your own site. If you scroll down, you can sign up for the newsletter, vote for the article, and comment.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

View Comments

  • I know it might be out of the scope of your article but it might be useful to add that when used in an if statement that || means "or" and && means "and". And there's also true and false expressions where they can be used.

Recent Posts

Is Your Storage Drive An SSD Or An HDD?

Today's article aims to answer a simple question, simply is your storage drive an SSD…

2 hours ago

Monitor System Resources With “Resources”

Today is one of those days when I'd like to introduce you to new software…

2 days ago

Short: The Halt Command

Today's article is intentionally short because we're simply going to learn about the basics of…

4 days ago

Installing Flatpaks In Linux

Today we're going to have a pretty easy article where you'll learn about installing Flatpaks…

6 days ago

A Quick Look At The Shutdown Command

This is going to be just a quick article about a command many of you…

1 week ago

Setting Up Coding Environments on Linux for Educational Use

With so many strong attributes, such as robustness and flexibility, Linux stands as a powerful…

1 week ago