Where does BASH_ENV get set?

Bodisha

New Member
Joined
May 19, 2019
Messages
13
Reaction score
1
Credits
151
I'm trying to understand how the $BASH_ENV variable gets set in a Red Hat installation. I've read the GNU page on Bash Startup Files. It states it's Invoked non-interactively and says

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

and goes on to say

As noted above, if a non-interactive shell is invoked with the --login option, Bash attempts to read and execute commands from the login shell startup files.

But it doesn't say how BASH_ENV is read into the environment. I suspect it means when a Bash shell is invoked in a non-interactive login mode, it reads both /etc/bashrc and ~/.bashrc, but I'm not seeing anything directly referencing that.

I'm also not seeing anything about how BASH_ENV is read in non-interactive non-login mode (Which is what I'm actually curious about).

Can someone clear this up for me? Thanks!
 
Last edited:


Usually in a shell script, the first line looks something like this...

#!/usr/bin/bash

or maybe

#!/bin/bash

/etc/bashrc is generic for all users.

/home/user/.bashrc is user specific

also some distros prefer to use...

/home/user/.bash_profile

So it can be in the profile file also.

The other thing here, is if you are running this as a switch user (sudo ) you need a flag to grab your environment.

For example...

sudo su fred

will change you to user fred, but it won't grab his environment variables.

so you add the "-" ( dash )

sudo su - fred

That will grab the variables in his .bashrc and .bash_profile.
 
Thanks for the reply. Unfortunately, I'm still a bit confused on on the subject. I hope you're ok with some follow up questions.

1) It sounds like you're saying if a script is getting started, the shell in the shebang will read the startup /etc/bashrc & ~/.bashrc scripts in non-interactive non-login mode. Is this correct?

2) In Red Hat, the /etc/profile calls the /etc/bashrc at the end. If BASH_ENV is set in /etc/bashrc, does this variable get expanded in any login mode with the file contents read? Base on what I've read, I have the impression Bash will only act on this variable if it's in non-interactive mode... But I haven't actually seen this anywhere.

3) What about an instance of cron starting a program? Or a child process being started? Does $BASH_ENV get set in those scenarios? If so how?

And thanks for the help!
 
In Linux, BASH_ENV is an environment variable that can be used to specify the path to a Bash startup script that is executed every time a new non-interactive Bash shell is started. This variable is not set by default, but you can set it to the path of a script if you want to configure your Bash environment for non-interactive sessions.

Non-interactive Bash sessions are typically used in scripts or in some system-level operations where a full interactive shell is not required, and you want to set certain environment variables or configurations for these non-interactive sessions.

For example, if you want to ensure that a specific script or configuration file is run for every non-interactive Bash shell session, you can set the BASH_ENV variable in your .bashrc or .bash_profile file like this:

(Put the following line in your bash_profile or bashrc)
export BASH_ENV="/path/to/my_script.sh"

Then, when a non-interactive Bash shell is started, it will execute the script located at "/path/to/my_script.sh" before running any other commands.

This can be useful for setting environment variables, configuring paths, or performing other initialization tasks for non-interactive scripts or automated tasks that use Bash.

In Red Hat, the /etc/profile calls the /etc/bashrc at the end.

It depends on if it is interactive or non-interactive.

# Bash login shells run only /etc/profile
# Bash non-login shells run only /etc/bashrc
 
Last edited:

Members online


Latest posts

Top