EN
Bash - run one command after second one when first one fails
0 points
In this short artlcle we would like to show how to run one command after second one when first one fails using Bash.
Quick solution:
xxxxxxxxxx
1
first_command || second_command
In this example, we present ls
and echo
commands combining.
Bash command:
xxxxxxxxxx
1
ls /path/to/unexisting/directory || echo "ls command has failed and both commands are done!"
Output:
xxxxxxxxxx
1
ls: cannot access '/path/to/unexisting/directory': No such file or directory
2
ls command has failed and both commands are done!
Explaination:
-
ls
command tries to list files under not existing directory and fails, -
then echo
command
is run printing text in the command line.