1

I am writing a bash script for the installation of an environment manager but it means that I have to install something within multiple directories. See my other question for more details. But this led me to rethink my approach.

I was now thinking of making it 3 separate bash scripts. Then the one will just call the other. Here is the thing: I need to some how source .bash_profile within the parent shell. The question is, would a bash script that has been called within a bash script be running in a new shell or the parent shell or would it be in a new shell within the host bash script shell within the parent shell?

'shell-ception' I know its cooking my brain. i would really like to automate this process and this is the wall I am facing.

3

If you execute a command in a shell it is always executed as a child process to this shell unless

  1. it is a shell builtin (like cd)
  2. it is sourced (. /path/to/file, source /path/to/file)
  3. it is executed via exec

You can easily check that with ps -e -o pid,ppid,user,args.

source .bash_profile influences subshells only if the variables have been marked for export (see declare -x).

  • In most shells (cmd) (where cmd is not builtin) doesn't fork an additional process. More generally in (cmd1; ...; cmdn), cmdn is executed by the subshell process (exception to that (for most shells) is when a trap is set in the subshell). – Stéphane Chazelas May 30 '13 at 12:12
  • @StephaneChazelas Strange; I must have wrongly remembered that. I had a look at that situation a few days ago. But maybe something was different then and I misinterpreted the special case for the general one. Good that someone proofreads... – Hauke Laging May 30 '13 at 12:18
2

This are the practical differences:

  • Running a command makes the shell to create a child process. Any environment variable previously defined in the child is lost to the parent process.

  • Using source the script is forced to run in the current process (within the parent process). Then, the environment variables in the script will remain.

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.