Questions tagged [exit-status]

Use this tag If your question revolves around determining or utilizing the exit status (return code) of a command. Common syntax involves the $? variable and the && and || symbols.

The exit status is an 8-bit integer value that is returned by a process; it is commonly used to determine success or failure of that process.

For further reading, see the POSIX page on Status Information.

To query the exit status of the most recent command, use the $? variable.

To execute a second command only if the first command was successful (returned a status of 0), use:

command-one && command-two

To execute a second command only if the first command failed (returned a non-zero status), use:

command-one || command-two

For more examples, see: What are the shell's control and redirection operators?

265 questions
96
votes
3 answers

Bash: run command2 if command1 fails

I want to do something like this: if cmd1 && cmd2 echo success else echo epic fail fi How should I do it?
michelemarcon
  • 3,357
  • 10
  • 32
  • 37
82
votes
4 answers

Default exit code when process is terminated?

When a process is killed with a handle-able signal like SIGINT or SIGTERM but it does not handle the signal, what will be the exit code of the process? What about for unhandle-able signals like SIGKILL? From what I can tell, killing a process with…
Cory Klein
  • 18,391
  • 26
  • 81
  • 93
71
votes
4 answers

What is the min and max values of exit codes in Linux?

What is the min and max values of the following exit codes in Linux: The exit code returned from a binary executable (for example: a C program). The exit code returned from a bash script (when calling exit). The exit code returned from a function…
user271801
  • 709
  • 1
  • 5
  • 4
67
votes
1 answer

Is there a standard command that always exits with a failure?

I want to test my script with a command that fails. I could use an existing command with bad arguments. I could also write a simple script that immediately exits with a failure. Both of these are easy to do and work for me, but if there is a…
Matthew
  • 5,417
  • 4
  • 19
  • 11
46
votes
2 answers

Exit code at the end of a bash script

I am confused about the meaning of the exit code in the end of a bash script: I know that exit code 0 means that it finished successfully, and that there are many more exit codes numbers (127 if I'm not mistaken?) My question is about when seeing…
soBusted
  • 621
  • 2
  • 7
  • 12
38
votes
5 answers

Unix command that immediately returns a particular return code?

Is there a standard Unix command that does something similar to my example below $ 56 $ echo Return code was $? Return code was 56 $ should be something that can be fork-execed and leaves 56 as the exit code when the process…
Chris
  • 813
  • 2
  • 7
  • 11
35
votes
1 answer

Can I get the exit code from a sub shell launched with $(command)?

I am setting a variable like this: myvar=$(command --params) I want to check the exit code ($?) of my command afterwards. Checking $? like this always returns 0 because it successfully set the variable to the output of the command. Is it possible…
Questionmark
  • 3,885
  • 8
  • 37
  • 57
26
votes
1 answer

Finding files for which a command fails

I would like to recursively find all the files for which a script which accepts a file as an argument returns a non-zero value. Any idea how to do this using 'find' or a similar tool?
mitanyen
  • 363
  • 2
  • 4
25
votes
5 answers

What does typing a single exclamation mark do in Bash?

Bash uses exclamation marks for history expansions, as explained in the answers to this question (e.g. sudo !! runs the previous command-line with sudo). However, I can't find anywhere that explains what running the following command (i.e. a single…
ash
  • 671
  • 6
  • 16
24
votes
7 answers

How does one extract a command's exit status into a variable?

I started learning Bash a couple of days ago. I'm trying to obtain an exit status of grep expression into a variable like this: check=grep -ci 'text' file.sh and the output that I got is No command '-ic' found Should I do it with a pipe command?
omri gilhar
  • 343
  • 1
  • 2
  • 5
20
votes
4 answers

Save exit code for later

So I have a little script for running some tests. javac *.java && java -ea Test rm -f *.class Now the problem with this is that when I run the script ./test, it will return a success exit code even if the test fails because rm -f *.class…
math4tots
  • 2,665
  • 8
  • 32
  • 42
17
votes
3 answers

Why does "file xxx.src" lead to "cannot open `xxx.src' (No such file or directory)" but has an exit status of 0 (success)?

Why does file xxx.src lead to cannot open `xxx.src' (No such file or directory) but has an exit status of 0 (success)? $ file xxx.src ; echo $? xxx.src: cannot open `xxx.src' (No such file or directory) 0 Note: to compare with ls: $ ls xxx.src ;…
pmor
  • 509
  • 5
  • 11
17
votes
2 answers

What does echo $? do?

In my terminal it printed out a seemingly random number 127. I think it is printing some variable's value and to check my suspicion, I defined a new variable v=4. Running echo $? again gave me 0 as output. I'm confused as I was expecting 4 to be the…
Weezy
  • 529
  • 1
  • 6
  • 16
17
votes
2 answers

How is the return status of a variable assignment determined?

I have seen constructs in scripts such as this: if somevar="$(somecommand 2>/dev/null)"; then ... fi Is this documented somewhere? How is the return status of a variable determined and how does it relate to command substitution? (For instance,…
Wildcard
  • 35,316
  • 26
  • 130
  • 258
16
votes
5 answers

get output and return value of grep in single operation in bash

I am writing a bash script; I execute a certain command and grep. pfiles $1 2> /dev/null | grep name # $1 Process Id The response will be something like: sockname: AF_INET6 ::ffff:10.10.50.28 port: 22 peername: AF_INET6 ::ffff:10.16.6.150 …
ilansch
  • 405
  • 1
  • 7
  • 23
1
2 3
17 18