what does '$?' mean in a shell script?

前端 未结 6 1900
抹茶落季
抹茶落季 2021-02-02 00:31

I came across a shell script that contains a statement like,

if [ $val -eq $? ]

What does $? mean here?

相关标签:
6条回答
  • 2021-02-02 00:56

    ls *.* or ls would produce the same result. Meaning show zero or more files with any extension in the current directory.

    echo $? would display the exit status. If at least one file is displayed from the last command ,the exit status would be zero(success).

    0 讨论(0)
  • 2021-02-02 00:58

    What does $? mean here?

    $? is the last result of an exit-status ... 0 is by default "successfull"

    bash# ls *.*
    bash# echo $? 
    bash# 0
    bash# ls /tmp/not/existing/
    bash# echo $?
    bash# 2
    
    0 讨论(0)
  • 2021-02-02 01:06

    This is the value of the exit status of the previous command. This is 0 in case of success.

    0 讨论(0)
  • 2021-02-02 01:17

    $# = number of arguments. Answer is 3.

    $@ = what parameters were passed. Answer is 1 2 3.

    $? = was last command successful. Answer is 0 which means 'yes'.

    0 讨论(0)
  • 2021-02-02 01:20
    $?
    

    returns the status of the last finished command. Status 0 tells you that everything finished ok.

    In addition the $ sign is a special symbol - and in that case $val extract the value that is hold by the variable val

    0 讨论(0)
  • 2021-02-02 01:21

    I found that the link is very useful and is the great answer. It includes clearly expression with sample.

    enter image description here

    0 讨论(0)
提交回复
热议问题