How do I set $? or the return code in Bash?
false
always returns an exit code of 1.
#!/bin/bash
false
while [ $? -eq 1 ]
do
#do something until it returns 0
done
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.