Languages
[Edit]
EN

Bash - run one command after second one regardless of the result

0 points
Created by:
Hiba-Tate
548

In this short artlcle we would like to show how to run one command after second one regardless of the result using Bash.

Quick solution:

first_command; second_command

 

Practical examples

Example 1 - both commands success

In this example, we combine sleep and echo commands.

Bash command:

sleep 3; echo "Sleep command done!"

Output:

Sleep command done!

Explaination:

  1. sleep command executes successfully suspends the calling shell script for 3 secons,

  2. then echo command is executed printing text in the command line.

Example 2 - fail & success

In this example, we use ls and echo commands .

Bash command:

ls /path/to/unexisting/directory; echo "echo command done!"

Output:

ls: cannot access '/path/to/unexisting/directory': No such file or directory
ls command has failed and done!

Explaination:

  1. ls command tries to list files under not existing directory and fails,

  2. then echo command is run printing text in the command line.

 

See also

  1. Bash - run one command after second one when first one is succeed and done

  2. Bash - run one command after second one when first one fails

Alternative titles

  1. Bash - run a command after the previous finished regardless of the result
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join