0

There are at least 3 possible ways of calling a bash script:

:~$./myscript
:~$bash ./myscript
:~$bash -c "./myscript"

One said that in the case #3 invocation (-c option) there is one more bash process in the hierarchy. I've prepared a simple test script:

echo "SHLVL: $SHLVL"  
ps aux | grep bash

The variable SHLVL is indeed greater by one in the #3 case, but the ps doesn't output any extra bash process in the #3 case. The only bash process connected to the script is

bin/bash ./testscript

So where is this extra bash process?

1

Actually, there seem to be an optimization in bash.

Lets make a sleep20s script:

#! /bin/bash
sleep 20

To avoid ambiguities, lets start a ksh shell.

With this command: bash -c "./sleep20s", you get:

ksh
  sleep20s
    sleep

Ooops, where is bash? But know if we do something that requires the bash interpreter, such as calling the same command twice: bash -c "./sleep20s;./sleep20s", then you get:

ksh
  bash
    sleep20s
      sleep

And bash is back. So bash could be clever and use an exec to start the child script in the trivial cases.

  • "So bash could be clever and use an exec to start the child script" - You mean just replacing bash code with scrip code as exec does? – Mulligan Jun 2 '17 at 20:54
  • @mulligan exacty – xenoid Jun 2 '17 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.