Cannot set tcsh exit status using a variable

你说的曾经没有我的故事 提交于 2019-12-11 02:12:46

问题


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?


回答1:


$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


来源:https://stackoverflow.com/questions/39775689/cannot-set-tcsh-exit-status-using-a-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!