Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

How can I do something like this in bash?

if "`command` returns any error";
    echo "Returned an error"
    echo "Proceed..."
share|improve this question

migrated from Oct 16 '11 at 22:29

This question came from our site for professional programmers interested in conceptual questions about software development.

5 Answers 5

That's exactly what bash's if statement does:

if command ; then
    echo "Command succeeded"
    echo "Command failed"

Adding information from comments: you don't need to use the [ ... ] syntax in this case. [ is itself a command, very nearly equivalent to test. It's probably the most common command to use in an if, which can lead to the assumption that it's part of the shell's syntax. But if you want to test whether a command succeeded or not, use the command itself directly with if, as shown above.

share|improve this answer
Note that the semicolon is important. –  Thorbjørn Ravn Andersen Oct 17 '11 at 7:37
Or you can just put then on a separate line. –  l0b0 Oct 17 '11 at 9:00
For the negative that the Op wants: if [ ! command ] ; then ... –  Joe Oct 22 '11 at 14:05
@Joe: I think you mean if ! command ; then ... ; fi. [ is itself a command, and it's not needed in this case. –  Keith Thompson Jan 13 '12 at 10:19
@Joe: My way also has the virtue of being correct. if [ ! command ] doesn't execute command; it treats command as a string and treats it as true because it has a non-zero length. [ is a synonym for the test command –  Keith Thompson Jan 14 '12 at 9:36

For small things that you want to happen if a shell command works, you can use the && construct:

rm -rf somedir && trace_output "Removed the directory"

Similarly for small things that you want to happen when a shell comand fails, you can use ||:

rm -rf somedir || exit_on_error "Failed to remove the directory"

It's probably unwise to do very much with these constructs, but they can on occasion make the flow of control a lot clearer.

share|improve this answer

Check the value of $?, which contains the result of executing the most recent command/function:


echo "this will work"
if [ $RESULT -eq 0 ]; then
  echo success
  echo failed

if [ $RESULT == 0 ]; then
  echo success 2
  echo failed 2
share|improve this answer
While technically correct (and thus not warranting a downvote), it's not making use of Bash's if idiom. I prefer Keith Thompson's answer. –  janmoesen Oct 17 '11 at 11:30

This worked for me:

command && echo "OK" || echo "NOK"

if command succeeds, then echo "OK" is executed, and since it's successful, execution stops there. Otherwise, && is skipped, and echo "NOK" is executed.

share|improve this answer
If you want to do something if it fails, and preserve the exit code (to show in command prompt or test in a script), you can do this: command && echo "OK" || c=$?; echo "NOK"; $(exit $c) –  Sam Hasler Jun 24 '14 at 15:57
@Sam-Hasler: shouldn't that be command && echo "OK" || (c=$?; echo "NOK"; (exit $c))? –  jrw32982 Apr 1 at 18:24
Also, if the echo "OK" part could itself fail, then this is better: command && (echo "OK"; exit 0) || (c=$?; echo "NOK"; (exit $c)) –  jrw32982 Apr 1 at 18:34
@jrw32982, Nice, I've used the former construction, but not the latter. –  Sam Hasler Apr 2 at 16:11

You can do this:

if ($( ping -c1 > /dev/null )) ; then
  echo "ping response succsess!!!"
share|improve this answer
That works but is convoluted. You're running ping in a subshell of a subshell, the output of ping is captured in view of running it as a command. But because the output is redirected to /dev/null that will always be the empty string. So you're running nothing in a subshell, which means the previous exit status (of the command substitution subshell, that is of ping) will be retained. Obviously, the correct way is if ping ...; then here. –  Stéphane Chazelas Apr 1 at 15:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.