1.

What Is The Use Of “$?” Sign In Shell Script?

Answer»

While writing a shell script, if you want to check whether PREVIOUS command is executed successfully or not, then we can use “$?” with if statement to check the EXIT status of previous command.

Basic example is SHOWN below:

root@localhost:~# ls /usr/bin/shar

/usr/bin/shar

root@localhost:~# echo $

0

If exit status is 0 , then command is executed successfully

root@localhost:~# ls /usr/bin/share

ls: cannot ACCESS /usr/bin/share: No such file or directory

root@localhost:~# echo $

2

If the exit status is other than 0, then we can say command is not executed successfully.

While writing a shell script, if you want to check whether previous command is executed successfully or not, then we can use “$?” with if statement to check the exit status of previous command.

Basic example is shown below:

root@localhost:~# ls /usr/bin/shar

/usr/bin/shar

root@localhost:~# echo $

0

If exit status is 0 , then command is executed successfully

root@localhost:~# ls /usr/bin/share

ls: cannot access /usr/bin/share: No such file or directory

root@localhost:~# echo $

2

If the exit status is other than 0, then we can say command is not executed successfully.



Discussion

No Comment Found