How to run multiple terminal commands in one line in Ubuntu / Linux?

Writing multiple commands together is known as Command Chaining, in this article I will show you different methods to combine multiple commands on terminal / shell.

Using Semicolon (;)

If you combine multiple commands using semicolon (;) operator, commands will execute even if the previous command in chain succeeds or fails.

In following example command2 will execute regardless of the command1’s success.


Here is a more practical example.

ping -c 3; whoami; pwd

Above chain of commands will:

  • ping: ping 3 times.
  • whoami: print user name of currently logged in user.
  • pwd: print current working directory.

Using AND Operator (&&)

Chaining multiple commands using AND (&&) operator will execute the command only and only if preceding command executes successfully.

command2 from following snippet will only execute if command1 runs successfully.

command1 && command2

From following listing the cd newDirectory command will only execute if mkdir newDirectory command was successful.

mkdir newDirectory && cd newDirectory

Using OR Operator (||)

Using OR ( ) operator makes sure that the following command executes only and only if preceding command fails.

command2 from following snippet will only execute if command1 fails to execute.

command1 || command2

From following example the echo command will only execute if ping command fails which means you are facing internet connectivity issue.

ping -c 3  || echo "You are not connected to internet"

Using NOT Operator (!)

You can use NOT (!) operator to exclude some results, following is an example command to delete all files in a directory except with .text extension.

Important Note: if you are trying this example, please do it in a practice / temporary directory to avoid deletion of actual / important work files.

rm -r !(*.text)

Combining AND (&&) and OR (||) operators

You can combine AND (&&) and OR (||)operators to simulate if ... elsescenario, let’s rewrite the *OR ( ) operator* example to print both SUCCESS and FAILURE message for internet connectivity check.
ping -c 3 google.com1 && echo "Your internet connection is OK"  || echo "You are not connected to internet"

That’s it, hope you enjoyed it. This was part one of using operators on linux command line shell /terminal, in next part of this article I will show more operators to enhance your productivity while working with linux shell commands. You like this article, have some questions or suggestions please let us know in the comments section.

Thanks and Happy Learning!

Shoket Mahmood Ahmed

60 thoughts on “How to run multiple terminal commands in one line in Ubuntu / Linux?

  1. Hello there, I discovered your blog by the use
    of Google whilst looking for a related subject,
    your web site got here up, it looks good. I have bookmarked
    it in my google bookmarks.
    Hi there, just became alert to your blog thru
    Google, and located that it’s truly informative.
    I’m going to be careful for brussels. I’ll be grateful when you
    continue this in future. Lots of other folks will probably be benefited out of your writing.

  2. Hey I know this is off topic but I was wondering if you knew of any widgets I could add
    to my blog that automatically tweet my newest twitter
    updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you
    would have some experience with something like this.
    Please let me know if you run into anything. I truly enjoy
    reading your blog and I look forward to your new updates.

  3. What i don’t realize is in truth how you’re not really
    a lot more neatly-preferred than you might be right now.
    You’re so intelligent. You know therefore considerably in relation to this
    matter, made me in my view consider it from a lot of varied angles.
    Its like men and women aren’t fascinated unless it’s one thing
    to accomplish with Lady gaga! Your own stuffs nice.
    All the time care for it up!

  4. Unquestionably consider that which you stated. Your favorite justification appeared
    to be at the internet the easiest factor to take note of.

    I say to you, I certainly get irked even as other folks think about issues that they plainly don’t recognise about.
    You controlled to hit the nail upon the top as well as defined out the entire thing with no need
    side effect , other folks can take a signal.
    Will probably be again to get more. Thanks

  5. When I initially left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox
    and now whenever a comment is added I recieve 4 emails with the same comment.
    There has to be a way you can remove me from that service?

    My homepage xe88 best game

  6. What’s Happening i’m new to this, I stumbled upon this I have found It
    absolutely useful and it has helped me out loads.
    I hope to contribute & assist other customers
    like its aided me. Great job.

  7. I am really impressed with your writing skills as well as with
    the layout on your weblog. Is this a paid theme or did you customize it yourself?

    Either way keep up the excellent quality writing,
    it is rare to see a great blog like this one these days.

  8. We stumbled over here different web address and thought I might as well check things out.
    I like what I see so i am just following you. Look forward to looking over your web page

  9. I’m really enjoying the design and layout of your blog.
    It’s a very easy on the eyes which makes it much more pleasant for me to
    come here and visit more often. Did you hire out a developer
    to create your theme? Exceptional work!

  10. Hmm it looks like your site ate my first comment (it was
    extremely long) so I guess I’ll just sum it up what I submitted and say, I’m thoroughly enjoying
    your blog. I as well am an aspiring blog writer but I’m
    still new to everything. Do you have any tips and hints for
    novice blog writers? I’d certainly appreciate it.

  11. Awesome blog you have here but I was curious if you knew of any message boards that cover
    the same topics discussed in this article?
    I’d really like to be a part of community where I can get opinions from other experienced people that
    share the same interest. If you have any recommendations, please
    let me know. Cheers!

    Stop by my blog post :: mega888 for ios

  12. I really love your blog.. Great colors & theme.
    Did you build this site yourself? Please reply back as
    I’m attempting to create my very own blog and want to know
    where you got this from or what the theme is
    called. Cheers!

  13. Do you mind if I quote a few of your articles as long
    as I provide credit and sources back to your blog?
    My blog site is in the exact same area of interest as
    yours and my users would genuinely benefit from a lot of the
    information you present here. Please let me know if this alright with you.
    Appreciate it!

  14. I’m extremely pleased to uncover this site.
    I wanted to thank you for your time for this particularly wonderful read!!
    I definitely liked every little bit of it and i also
    have you saved as a favorite to check out new information on your site.

  15. Do you mind if I quote a couple of your posts as long as I provide credit
    and sources back to your webpage? My website is in the very same niche
    as yours and my visitors would really benefit from some of the information you provide here.
    Please let me know if this alright with you.

    Many thanks!

  16. I seriously love your website.. Excellent colors & theme.

    Did you make this website yourself? Please reply back as I’m wanting to
    create my own site and want to know where you got this from or what the theme is called.
    Thank you!

  17. Hi, I think your site could be having internet browser compatibility issues.
    When I take a look at your site in Safari, it looks fine however when opening in I.E., it’s got some overlapping
    issues. I merely wanted to give you a quick heads up!
    Apart from that, fantastic site!

  18. It’s a pity you don’t have a donate button! I’d certainly donate to this excellent blog!

    I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to fresh updates and will talk about this site with my Facebook
    group. Chat soon!

  19. hello there and thank you for your info – I have certainly picked up something new from right here.

    I did however expertise a few technical points using this web site, as I experienced to reload the site
    many times previous to I could get it to load properly.
    I had been wondering if your web hosting is OK? Not that I’m
    complaining, but slow loading instances times will very frequently affect your placement
    in google and can damage your quality score if advertising and
    marketing with Adwords. Well I’m adding this RSS to
    my e-mail and can look out for much more of your respective fascinating
    content. Ensure that you update this again soon.

  20. Ι think the admin of this site is reazlly working hard in support of his web site, sіnce heree eveгy
    stuff is quality based stuff.

  21. Excellent article. Keep writing such kind of information on your page.
    Im really impressed by it.
    Hi there, You’ve done a great job. I’ll certainly digg it and personally suggest to my friends.
    I’m confident they will be benefited from this web site.

  22. I am no longer positive where you are getting your info, but
    great topic. I must spend some time finding out much more or figuring out more.
    Thank you for wonderful information I was searching for
    this info for my mission.

Leave a Reply

Back to top