4

A non-login interactive shell (for example: when I open a Terminal window in Ubuntu) loads and executes the file ~/.bashrc.

Now when I execute a shell script, a non-interactive non-login shell process will be created that will run this shell script.

Is there a file that is loaded and executed by this non-interactive non-login shell?


Edit: The shell I am using is bash.

3
  • How exactly do you execute that shell script? It could matter... Sep 24, 2017 at 11:16
  • @Basile Starynkevitch I am executing the shell script from the Terminal window (./my_shell_script.sh).
    – Tom
    Sep 24, 2017 at 11:26
  • A nice description of files sourced
    – user232326
    Sep 25, 2017 at 1:07

2 Answers 2

3

From man bash, in a non-interactive shell the BASH_ENV environment variable is evaluated and its content is sourced. So you could use that environment variable to specify a file with your environment settings.
By default no files will be sourced in non-interactive mode.

When bash is started non-interactively, to run a shell script, for example, it looks for the variable BASH_ENV
in  the  environment, expands its value if it appears there, and uses the expanded value as the name of a file 
to read and execute.  Bash behaves as if the following command were executed:
       if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search for the filename.
2

When you execute some shell script with a non-interactive non-login shell, notably by using system(3) in some C program (or by running it from crontab(5)), no file is implicitly loaded.

Your script might source some file, using . or source shell builtin.

Perhaps you should care about which environment (see environ(7)) or what file descriptors are relevant, and that depends on how your shell script is started, what are the ancestor processes, etc. If you run your script from a shell in a terminal window, it inherits many things from your shell (notably your $PATH). However, if you run that script differently (in some init or systemd script, thru cron or batch or nohup, ....) things can be different.

See also daemon(3), tty(4) ....

Read fork(2), execve(2) (both are used in your shell), credentials(7)

Read chapter on Bash startup files.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .