Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set $? or the return code in Bash?

I want to set a return value once so it goes into the while loop:

#!/bin/bash while [ $? -eq 1 ] do #do something until it returns 0     done 

In order to get this working I need to set $? = 1 at the beginning, but that doesn't work.

like image 323
JohnnyFromBF Avatar asked May 04 '12 11:05

JohnnyFromBF


People also ask

How do I set an exit code in bash?

To set an exit code in a script use exit 0 where 0 is the number you want to return. In the following example a shell script exits with a 1 . This file is saved as exit.sh . Executing this script shows that the exit code is correctly set.

How do you return a value in bash?

When a bash function completes, its return value is the status of the last statement executed in the function, 0 for success and non-zero decimal number in the 1 - 255 range for failure. The return status can be specified by using the return keyword, and it is assigned to the variable $? .

What does $? Stand for in bash script?

$? gives you the return code of the previous command executed. $# give you the number of command line arguments given to the script.


1 Answers

You can set an arbitrary exit code by executing exit with an argument in a subshell.

$ (exit 42); echo "$?" 42 

So you could do:

(exit 1)    # or some other value > 0 or use false as others have suggested while (($?)) do     # do something until it returns 0     done 

Or you can emulate a do while loop:

while      # do some stuff     # do some more stuff     # do something until it returns 0     do     continue  # just let the body of the while be a no-op done 

Either of those guarantee that the loop is run at least one time which I believe is what your goal is.

For completeness, exit and return each accept an optional argument which is an integer (positive, negative or zero) which sets the return code as the remainder of the integer after division by 256. The current shell (or script or subshell*) is exited using exit and a function is exited using return.

Examples:

$ (exit -2); echo "$?" 254 $ foo () { return 2000; }; foo; echo $? 208 

* This is true even for subshells which are created by pipes (except when both job control is disabled and lastpipe is enabled):

$ echo foo | while read -r s; do echo "$s"; exit 333; done; echo "$?" 77 

Note that it's better to use break to leave loops, but its argument is for the number of levels of loops to break out of rather than a return code.

Job control is disabled using set +m, set +o monitor or shopt -u -o monitor. To enable lastpipe do shopt -s laspipe. If you do both of those, the exit in the preceding example will cause the while loop and the containing shell to both exit and the final echo there will not be performed.

like image 154
Dennis Williamson Avatar answered Oct 07 '22 15:10

Dennis Williamson