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
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.
; operator can be used something like this:
cd Downloads ; touch Foo
&& operator can be used something like this:
sudo apt update && sudo apt upgrade -y
|| 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.
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.
Last Updated on January 21, 2022 by KGIII