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

Time:10-14

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 "$@"

CodePudding user response:

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, i.e. 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 .

  • Related