Does a bash script have return code itself? Does a bash script have return code itself? bash bash

Does a bash script have return code itself?


  1. Yes, or the value passed after exit, e.g. exit 31.

  2. Not without taking measures within the other script to make it explicit.


$? reads the exit status of the last command executed. After a function returns, $? gives the exit status of the last command executed in the function. This is Bash's way of giving functions a "return value.

Example

#!/bin/bashecho helloecho $?    # Exit status 0 returned because command executed successfully.lskdf      # Unrecognized command.echo $?    # Non-zero exit status returned because command failed to execute.echoexit 113   # Will return 113 to shell.       # To verify this, type "echo $?" after script terminates.#  By convention, an 'exit 0' indicates success,#+ while a non-zero exit value means an error or anomalous condition


the return code of the script is indeed the return code of the last command executed, some commands allow you to finish execution at any point and arbitrarily set the return code; those are exit for scripts and return for functions but in both cases if you omit the argument they'll just use the return code of the previous command.