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:
first_command || second_command
Practical example
In this example, we present ls and echo commands combining.
Bash command:
ls /path/to/unexisting/directory || echo "ls command has failed and both commands are done!"
Output:
ls: cannot access '/path/to/unexisting/directory': No such file or directory
ls command has failed and both commands are done!
Explaination:
-
lscommand tries to list files under not existing directory and fails, -
then echo
commandis run printing text in the command line.