我建立一个shell文件 script.sh,内容如下:
#!/bin/bash
exit 888
然后我再执行下面的语句
[root@localhost]# chmod u+x script.sh
[root@localhost]# ./script.sh
[root@localhost]# echo $? # ?为shell变量,代表最后执行的程序的返回值。用echo $解析出来
120
注:exit 不加参数表示,退出。返回值不变,即为最后一个命令的返回值。(If n is omitted, the exit status is that of the last command executed. )
exit n 加参数表示,退出。设置返回值为n。(Cause the shell to exit with a status of n.)
那么既然我们设置了exit 888,命令返回值就应该是888才对,为什么结果是120?
这是因为,Linux默认返回值限制为8bits。我们返回888,二进制为1101111000,超出了两位,从高位去掉两位即得01111000,也就是120.
可以再实验下其他的值。