DEV Community

tarohida
tarohida

Posted on

3 3

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)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay