How to use multiple commands after || in bash

Submit code:

ls || echo "Unable to execute ls (returned non zero)"

what if i need to execute more commands like:

ls || echo "this is echo 1" <some operator> echo "this is echo 2" <some operator> exit 1

in C (if I have the ls function), I could do (even if it looks crazy):

ls() || (command1() && command2());

but I doubt that parentheses can use parentheses in bash

edit: I know that I can create a bash function that will contain these commands, but what if I need to combine this with output 1 (exiting the function will end this function, not the whole script)

+4
source share
2 answers

You can group several teams in { }. Saying:

some_command || { command1; command2; }

would execute command1and command2if it some_commandcame out with a non-zero return code.

{}

{ list; }

, . . ( ) .

+9

. bash " ". :

ls || { echo 1 ; echo 2 ; }

, ; curlie .

+4

All Articles