DEV Community

BC
BC

Posted on

Run multiple commands in one line with `;`, `&&` and `||` - Linux Tips

There are 3 ways to run multiple shell commands in one line:

1) Use ;

No matter the first command cmd1 run successfully or not, always run the second command cmd2:

# cmd1; cmd2
$ cd myfolder; ls   # no matter cd to myfolder successfully, run ls
Enter fullscreen mode Exit fullscreen mode

2) Use &&

Only when the first command cmd1 run successfully, run the second command cmd2:

# cmd1 && cmd2
$ cd myfolder && ls  # run ls only after cd to myfolder
Enter fullscreen mode Exit fullscreen mode

3) Use ||

Only when the first command cmd1 failed to run, run the second command cmd2:

# cmd1 || cmd2
$ cd myfolder || ls  # if failed cd to myfolder, `ls` will run
Enter fullscreen mode Exit fullscreen mode

Top comments (12)

Collapse
 
thefluxapex profile image
Ian Pride • Edited

Ternary (or more) operations on a single line:

[[ 1 -eq 1 ]] && echo true || echo false

[[ "${var}" == "string" ]] && ([[ "$(id -un)" == "root" ]] && echo ROOT || echo USER) || echo false
Enter fullscreen mode Exit fullscreen mode

And can be multiple lines:

[[ 1 -eq 1 ]] &&
echo true ||
echo false

[[ "${var}" == "string" ]] &&
([[ "$(id -un)" == "root" ]] &&
    echo ROOT ||
    echo USER) ||
echo false
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ferricoxide profile image
Thomas H Jones II

I use the <CMD> && <SUCCESS_ACTION> || <FAIL_ACTION> method quite a lot ...annoying that BASH-linters complain about it.

Collapse
 
chaitanyashahare profile image
Chaitanya Shahare

Thanks for these examples, they are really helpful

Collapse
 
bitecode profile image
BC

Nice ๐Ÿ‘ thanks for the add-up, Ian

Collapse
 
thefluxapex profile image
Ian Pride

You're welcome; I just thought it would add a little perspective of how this concept can be used in various ways. Some might always prefer to use if/else statements to be safe & sometimes it is necessary, but I find myself using this type of 'shortcut' more times than not when it's safe to use.

Collapse
 
gokayburuc profile image
gokayburuc.dev

I was searching that for a long time. Thanks a lot.

I added it to .bashrc and .zshrc files with the alias commands . And now one command is enough for the maintain system operations.

alias maintain_sys="sudo apt update &&
 sudo apt upgrade && 
sudo apt autoclean" 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
topitguy profile image
Pankaj Sharma

Thanks for that

Collapse
 
bill_naylor_4e2aa2e312e7a profile image
Bill Naylor • Edited

I'm quite happy with using ';', '|', etc. however I generally am using them from an 'end-user' perspective. I frequently use commands like:

mvn clean package >& /tmp/out-comptest; less +G /tmp/out-comptest

then exit less, but may after want to see 'what was important in that file', is there a short hand way to run the last command of that sequence ... viz less +G /tmp/out-comptest?

Collapse
 
ahmednr_dev profile image
Nino

Thanks

Collapse
 
ajay_kumar profile image
Ajay Kumar

thanks

Collapse
 
stangelandm1 profile image
Michael Stangeland • Edited

Can this be nested?

A && (B && echo passed || echo B failed ) || echo A failed

Collapse
 
nem8 profile image
nem8

Sure, but you need to re-write it slightly..
echo 'Test' > A
cat A && echo 'A passed' || echo 'A failed' && (cat B && echo 'B passed' || echo 'B failed')
Test
A passed
cat: B: No such file or directory
B failed