简体   繁体   中英

Does git bash create a subshell by default when running a script file?

Im exploring a bit on shell scripts and wrote this script. When the build command fails, it has to provide an error message and exit.

I know that exit 1 would close the current shell its running on. And the exit statement is within a curly braces (meaning that its executed on the same shell). But when this script is run as ./testScript.sh it stops executing test(), testTwo() is not called, and the terminal remains open.

While this is exactly the functionality i'm looking for, my question is why doesn't it close the terminal due to the exit 1; command? Does git bash creates a subshell by default when running a script?

I know its not because its been called within a function. I did try running the exit statement in script without a function, and it still doesnt close the terminal.

Any insights on the actual working of git bash and exit command would be highly useful.

Thanks!

# testScript.sh

function test() {
    dotnet build -c Debug sample.csproj || { echo -e "${RED}=== Build Failed";  exit 1; }
}

function testTwo() {
    echo "== executing test two function."
}

function all()
{
    pushd .
  test
  testTwo
    popd
}

all "$@"

Does git bash creates a subshell by default when running a script?

It's not a "subshell" as in ( ) . The shell spawns a separate process that executes the command. The process does not inherit bash variables and functions, only inherits exported variables, ie it's not a subshell.

Do not use function name() , just name() . See https://wiki.bash-hackers.org/scripting/obsolete . Check scripts with shellcheck.net .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM