Is there any difference between these two commands:
exec "$(dirname "$0")/suman-shell";
exit $?;
and
exec "$(dirname "$0")/suman-shell";
exit;
is the $?
redundant in the first case?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIs there any difference between these two commands:
exec "$(dirname "$0")/suman-shell";
exit $?;
and
exec "$(dirname "$0")/suman-shell";
exit;
is the $?
redundant in the first case?
From help exit
:
Exits the shell with a status of N. If N is omitted, the exit status is that of the last command executed.
So yes, it is completely redundant.
exec
.
exit
present in the script isn't reached in the first place. exec
failing will directly exit the script bypassing any line that would follow.
There are several flaws in your script:
exec "$(dirname "$0")/suman-shell";
exit $?;
The first one is the ending semicolons are redundant. They are harmless but serve no purpose. An end of line is already a separator, semicolons are used as a separator between multiple commands on a single line. Their presence here is slightly worsen the code readability.
The second one is exit
by default returns the exit status of the previous command, so stating $?
is redundant, albeit signalling the intent.
The third one is exec
never returns under normal circumstances1, so the exit
call is not reached in the first place and is also redundant.
1 The only cases where exec
returns is an empty argument, a broken redirection or the execfail
bash option set (which is not the default setting). The first and second ones do not apply here, and there is no evidence the third one does.
;;;
Nov 8, 2017 at 20:39
bash
is concerned...
Strictly speaking, there's a difference between exit
and exit $?
when $IFS
contains digits when you forget to quote that $?
and thus are invoking the split+glob operator:
$ bash -c 'IFS=123; false; exit $?'; echo "$?"
bash: line 0: exit: : numeric argument required
2
$ bash -c 'IFS=123; false; exit'; echo "$?"
1
$ bash -c 'IFS=123; false; exit "$?"'; echo "$?"
1
$ bash -c "exit ''"
bash: line 0: exit: : numeric argument required
In the first case, because the $?
is unquoted, it's subject to word splitting. With $IFS
containing 1
, the content of $?
(1
) is split into one empty argument in bash
, so it's as if you had called exit ''
.
x; exit;
on the same line, yolo$?
do signal intent much more than the semicolons. Bash isn't C.