0

What is the correct way to output an exit status in bash? As far as I know, the exit status called by $? corresponds to the status of the last command executed.

The script being worked on has a few conditional checks on the files fed as arguments, for example, a check on whether any files were named at all or if a file exists or not.

So I have conditional statements like this:

if [ $# -eq 0 ] ; then echo "No file name(s) given! \nExit status=$?" exit if [ ! -e "$fName" ] ; then echo "$fName does not exist! \nExit status=$?" exit 

But these return an exit status of 0. I'm not entirely sure even what exit codes would be appropriate for each of these situations, but I think both 1 would work for both based on this. Should I just hard-code the 1 into the conditional statement or change the logic so unix outputs an error code? Also, what command would the 0 that I get for the above example be the exit code for?

11
  • BTW, echo too has an exit status, so by the time you get to the exit, your last exit status is that of the echo, so (presuming that the echo successfully wrote its output) exit's default behavior of exit "$?" will have the effect of exit 0, no matter what $? had been before echo ran. Commented Jun 27, 2018 at 2:51
  • 1
    As another aside -- I strongly, strongly recommend avoiding the ABS as a reference; when it's not outright wrong, it has a long history of showing bad practices in its examples. The bash-hackers' wiki and the BashGuide are far better sources, as of course is the official manual. Commented Jun 27, 2018 at 2:57
  • Besides the link, was there something in the code that looked like bad practice? Commented Jun 27, 2018 at 3:01
  • I was responding to the link, but using echo with backslash literals is unspecified by POSIX and has behavior that various across shells (or even across runtime configurations, such as varying on the state of the xpg_echo flag in bash); see the APPLICATION USAGE section of pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html Commented Jun 27, 2018 at 3:06
  • BTW, personally, I might write these checks more like (( $# )) || { echo "ERROR: No file names given!" >&2; exit 1; } (if we're using bashisms; [ "$#" -eq 0 ] && { ... otherwise) and [[ -e $fName ]] || { echo "ERROR: Could not find $fName" >&2; exit 1; } (here, using [[ rather than [ to get extended syntax making quotes unnecessary; again, an extension, but an explicit one that behaves consistently across ksh-inspired shells -- bash and zsh among them -- where it's available). Commented Jun 27, 2018 at 3:10

1 Answer 1

3

In both of these cases, the last command executed was [, which exited with 0 status ("success") to land you in the "true" block.

Yes, you need to specify a non-zero exit code for the exit command.

Pedantically "unix" does not output the error code: it's the shell running your script that exits with the specified status.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, it's a command, not mere syntax. It's also known as test. At an interactive bash prompt, enter help [ for the details.
I'd suggest starting with type -a [, which will provide enough information to know whether help (for shell builtins or keywords) or man (for external commands) is the next step. (Several commands are both shell builtins and external commands; the shell version is in such cases effectively a performance optimization).
Note that [[ ]] and (( )) are shell syntax (in bash and some other shells), but still affect $? as though they were commands.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.