I usually use bash, but in this case I must use tcsh. To my surprise, I cannot use a variable containing the exit status as the argument to exit
:
[bash] tcsh
[tcsh] set status=2
[tcsh] echo $status
2
[tcsh] exit $status
exit
[bash] echo $?
0
A literal argument to exit
does work as expected:
[bash] tcsh
[tcsh] exit 2
exit
[bash] echo $?
2
What on earth is going on here?
$status
is a built-in C shell variable containing the exit status of the previous command. Try echoing $status
twice and you can see that tcsh changes the value of $status
to 0, the exit status of the first echo
command:
[bash] tcsh
[tcsh] set status=2
[tcsh] echo $status
2
[tcsh] echo $status
0
The solution is to merely use a different variable name that is not a C shell built-in variable:
[bash] tcsh
[tcsh] set result=2
[tcsh] echo $result
2
[tcsh] exit $result
exit
[bash] echo $?
2