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.

48
votes
4answers
61k views

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 ...
41
votes
1answer
12k views

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 ...
37
votes
4answers
11k views

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 ...
36
votes
2answers
38k views

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?
27
votes
5answers
6k views

Unix command that immediately returns a particular return code?

Is there a standard Unix command that does something similar to my example below $ <cmd here> 56 $ echo Return code was $? Return code was 56 $ <cmd here> should be something that can be ...
26
votes
1answer
1k views

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?
20
votes
5answers
3k views

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 ...
19
votes
2answers
61k views

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 ...
14
votes
3answers
15k views

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 ...
11
votes
2answers
941 views

How to make `local` capture the exit code?

In my project I have the following snippet: local output="$(bash "${1##*/}")" echo "$?" This always prints zero due to local, however, removing local causes the $? variable to behave correctly: ...
11
votes
3answers
3k views

Why do I get different exit status for ps | grep in a script?

I am running below script : #!/bin/bash ps ax | grep -q [v]arnish if [ $? -eq 0 ];then echo varnish is running... exit 0 else echo "Critical : varnish is not running " ...
10
votes
5answers
3k views

Capture exit code of exit command

I have this in a bash script: exit 3; exit_code="$?" if [[ "$exit_code" != "0" ]]; then echo -e "${r2g_magenta}Your r2g process is exiting with code $exit_code.${r2g_no_color}"; exit "$...
10
votes
7answers
2k views

Log exit code of command, similar to time command

using time sleep 1 yields: $ time sleep 1 real 0m1.005s user 0m0.001s sys 0m0.001s is there a command I can use to print the exit code of sleep or whatever command I want to run? ...
10
votes
4answers
14k views

Bash script to detect the version control system by testing command return status

I am working on a bash script that I would like to work for several types of VCS. I am thinking of testing if a directory is a repo for a system by running a typical info command and checking the ...
10
votes
2answers
491 views

Assignments are like commands with an exit status except when there's command substitution?

See the following examples and their outputs in POSIX shells: false;echo $? or false || echo 1: 1 false;foo="bar";echo $? or foo="bar" && echo 0: 0 foo=$(false);echo $? or foo=$(false) || ...
9
votes
5answers
35k views

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 ...
9
votes
2answers
18k views

Differences between inactive vs disabled and active vs enabled services

I am confused by the concept of enabled or active and disabled or inactive. Could someone explain it?
9
votes
1answer
530 views

Cause `less` to exit with a non-zero status code?

I would like to have the following command structure: command && check-status | less && followup-command This would pause the execution while the user interacted with less. How can ...
8
votes
3answers
2k views

How can one extract the exit code of a command? [duplicate]

How can one extract the exit code of a command and use it in a condition? For example look at the following script: i=4 if [[ $i -eq 4 && $(command) -eq 0 ]] then echo "OK" ...
8
votes
6answers
11k views

How to keep last exit status after test

Is it possible to keep the last command exit status ($?) unaltered after a test? E.g., I would like to do: command -p sudo ... [ $? -ne 1 ] && exit $? The last exit $? should return the ...
8
votes
1answer
394 views

Why does exiting with a stored returncode of a nested command result in different returncodes in Dash and Bash?

Running bash -c 'bash -c "echo test1; exit 1;" &> /tmp/x; buildresult=$?; tail -n 100 /tmp/x; exit $buildresult;' results in test1 being printed to console and echo $? to print 1 which in my ...
8
votes
6answers
408 views

Elegant way to construct a pipeline based on return value and not exit code?

When the status code is useless, is there anyway to construct a pipeline based on output from stdout? I'd prefer the answer not address the use-case but the question in the scope of shell scripting. ...
8
votes
2answers
1k views

What is ENOANO (No Anode) intended to be used for?

This question is merely for idle curiosity, but I suspect others will be curious as well. Searching through errno.h (of Linux 2.6) I found ENOANO "No Anode". There is no sign of a "No cathode" error. ...
8
votes
2answers
1k views

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 ...
7
votes
2answers
2k views

How do I look up exit codes for applications?

I'm having difficulty locating a comprehensive up-to-date list of error codes from Bash. e.g.: $ udevadm info /dev/sdx; echo Exit code $? Unknown device, --name=, --path=, or absolute path in /dev/ ...
7
votes
3answers
2k views

Why does the exit status differ when running `systemctl start; systemctl is-active` and `systemctl is-active` separately?

The following sequence gives me the return value of the first command, not the 2nd as I would have expected (no matter if I run the 1st command in a subshell): sudo systemctl start x; sudo systemctl ...
7
votes
2answers
2k views

make find fail when nothing was found

When find is invoked to find nothing, it still exits with code 0. Is there a way to make it return an exit code indicating failure when no file was found?
7
votes
1answer
625 views

List of error messages available for a command in Unix

Is it possible to find out what error messages a command includes? For example I want to see the list of error messages that mkfs.ext3 command will print in different error situations. Shall I look ...
7
votes
3answers
178 views

Is there a way to get an process exit status after many more commands have been issued in linux?

If I have a lot of commands I've issued and I want the exit status of a process that exited say 100 commands ago. Is there a struct, file location, or variable in linux I can access all of exited ...
6
votes
2answers
8k views

return value from eval

The bash manual states: eval [arg ...] The args are read and concatenated together into a single com- mand. This command is then read and executed by the shell, and ...
6
votes
1answer
25k views

Return code for curl used in a command substitution

I've script as below :- (edited) httpUrl="http://www.nnin.com" rep=$(curl -v -X POST -d "UID=username&PWD=pass" $httpUrl) status=$? if [ "$?" -eq 0 ]; then echo "Success" exit $status ...
6
votes
2answers
13k views

What does gpg error code 2(GPG_ERR_UNKNOWN_PACKET) mean?

I am tasked to explain the variation of gpg errors that happened in one of my batch script. Currently when I perform gpg decrypt for a specified file it returns 2. The problem with this is when I ...
6
votes
2answers
4k views

How can I catch a command exit code for later, in a Makefile?

I have a makefile, with a test suite target, like so: rcheck: foo This shows the output from the foo command on stdout, and returns an exit code from foo which is handled by make as normal. ...
5
votes
5answers
4k views

Getting “ensure” / “finally” functionality in a shell command (not script)?

I need to know whether a command has succeeded or failed, and unconditionally run some cleanup afterward. Neither of the normal options for executing sequential commands seem to be applicable here: ...
5
votes
3answers
554 views

bash script executed over ssh returns incorrect exit code 0

I am trying to automate a process which involves running scripts on various machines via ssh. It is vital to capture both output and the return code (for the detection of errors). Setting the exit ...
5
votes
3answers
727 views

How to get further information about the origin of an exit code?

Sometimes I need to maintain programs that invoke shell scripts which invokes other programs and scripts. Therefore, when the main shell script ends with exit code 126, it is a struggle to find out ...
5
votes
4answers
32k views

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....
5
votes
2answers
4k views

Why to set errno to zero at the time of initialization of the program and can't test it before error occurred? [closed]

I am reading the material available on errno variable of <errno.h> header file. I read : " A value of 0 indicates that there is no error in the program." and "As a good practice, developer ...
5
votes
1answer
439 views

`find` return value: Is the find man page wrong?

The find man page says: -execdir command {} + always returns true, while -execdir command {} ; returns true only if command returns 0. and EXIT STATUS find exits ...
5
votes
1answer
2k views

Linux kernel action upon init process exiting

Say I am writing my own init program running on a Linux kernel. What happens when my init program exits with return value 0 ? Additionally is the behaviour different if the return value is non-zero?
4
votes
4answers
544 views

Bash - Execute two commands and get exit status 1 if first fails

In the case below, the report command must always be executed but I need to get an exit status 1 if the test command fails: test;report echo $? 0 How can I do it in a single bash line without ...
4
votes
2answers
3k views

Simultaneously check for empty output and successful exit status

I'd like to write the following test in an installer script1: if [ -n "`/etc/grub.d/30_os-prober`" ]; then install_dual_boot else install_linux_only fi However, it's also possible that 30_os-...
4
votes
1answer
4k views

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

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 ...
4
votes
1answer
16k views

Capture exit code and output of a command

I'd like to do: 1.sh: #!/usr/bin/env bash set -eu r=0 a=$(./2.sh || r=$?) echo "$a" echo "$r" 2.sh: #!/usr/bin/env bash echo output exit 2 But it outputs: $ ./1.sh output 0 # I'd like to have `...
4
votes
1answer
133 views

How to make `trap` know if the EXIT is after successful program finish or because of premature as an error or something

PROBLEM: I have a shell program that I have been writing but I can't find out how to make sure that trap is trapping for cleanup at the end or because of a error in some command, it cleans up either ...
4
votes
1answer
580 views

Failed to activate systemd-timesyncd.service

I've scanned all forums and questions and found no working answer to my problem. The Network Time Synchronization service doesn't work. The timedatectl service seems to work just fine without it ...
4
votes
2answers
382 views

How to run multiple processes and exit if any of them exits or fails

I am running multiple processes and want to exit with the appropriate exit code (this means error on failure, success otherwise) if any of them fails or exists. Additionally if any child process ...
4
votes
4answers
3k views

Provide default value if command returns with non-zero exit code

I have some configuration in file config and would like to cat that file. However, sometimes config doesn't exist. In this case, I would like to have my command output a default value. Perhaps ...
4
votes
1answer
97 views

Display output of `time` only if command after `time` was successful

I want the output of the time command to be shown only if the command, which has been passed to time was successful. Something like this: ( time wget -pq --delete-after https://www.example.com ) 2>...
4
votes
1answer
284 views

host returns 0 for a non existing name

host usually returns 0 for existing hosts $ host www.google.com ; echo $? www.google.com has address 172.217.16.132 www.google.com has IPv6 address 2a00:1450:400a:805::2004 0 and 1 in case of errors ...