How do i check the exit code after running a shell script?



  • Exit codes show if a script or program ran successfully. If the exit code is zero, it was successful, anything else is not successful.

    To check the exit code immediately after running a shell script do the following:

    $ ./myscript 
    My fruit is pineapple
    $ echo $?
    0
    

    The "$?" is a special shell variable to show the exit code.

    The exit code can also be seen using commands on the shell prompt. When you introduce a typo and check the exit code, you see 127

    $ whoami
    trainer
    $ echo $?
    0
    $ whoam
    No command 'whoam' found, did you mean:
     Command 'whom' from package 'mailutils-mh' (universe)
     Command 'whom' from package 'nmh' (universe)
     Command 'whoami' from package 'coreutils' (main)
    whoam: command not found
    $ echo $?
    127
    

Log in to reply
 

© Lightnetics 2024