0

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 "$@"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Niveditha Karmegam
  • 742
  • 11
  • 28
  • Your script has no shebang. Are you sure you can run it as `./testScript.sh`? – KamilCuk Oct 13 '21 at 17:56
  • Scripts run as _child processes_, but that's not the same as a subshell. – Charles Duffy Oct 13 '21 at 17:57
  • Also, the "snippet" button is only for HTML + JavaScript. Use the `{}` button for formatting code in other languages. – Charles Duffy Oct 13 '21 at 17:58
  • Anyhow -- because you're using bash-only syntax, this isn't properly a sh script -- it's specifically a bash script; not all copies of sh will run it. Really, it's best not to use extensions on executable names at all -- if you use the shebang to specify an interpreter, you can change that interpreter at will without needing to rename the file and thus modify programs that invoke it. See also [Commandname extensions considered harmful](https://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful/). – Charles Duffy Oct 13 '21 at 17:59

1 Answers1

2

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 .

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Thanks! Yes, I did notice that the execution creates new processes. So the exit statement kill the processes and thereby the execution is halted and the terminal is not closed. But why would it not execute the second function, im assuming it too should be on a new process? – Niveditha Karmegam Oct 13 '21 at 18:07
  • `m assuming it too should be on a new process?` As your observation hints you, your assumption is not valid. Function is executed within the current shell. – KamilCuk Oct 13 '21 at 18:23
  • 1
    @NivedithaKarmegam Whether the second function would run as a subprocess or not is irrelevant, since the script exits before starting it. When it exits, it stops executing the `test` function, the `all` script that called that, *and* the main sequence of the `testScript.sh` file. – Gordon Davisson Oct 13 '21 at 18:30