DEV Community

tarohida
tarohida

Posted on

When I run `exit` command in bash function, how it works.

Environment

  • bash

Problems

  • I don't know how it works when I exec exit command in bash function.
    • exit function or exit script?
    • What will the exit code look like?

How to solve

I actually did it, and found out below.

  • script execution ended.
  • exit code will be set to exit code specified by exit command.

Description

cat << EOF > a.sh
exit_func () {
    exit 0
}

exit_func
echo 'execute here!'
EOF

sh ./a.sh
echo $?
Enter fullscreen mode Exit fullscreen mode

output

0
Enter fullscreen mode Exit fullscreen mode
cat << EOF > b.sh
exit_func_254 () {
    exit 254
}

exit_func_254
echo 'execute here!'
EOF

sh ./b.sh
echo $?
Enter fullscreen mode Exit fullscreen mode

output

254
Enter fullscreen mode Exit fullscreen mode

Reference

Top comments (0)