I have the situation where I want to run a bashrc file on an OSX machine every time a shell is opened.
In this case, there is no login shell, and the shell is noninteractive. I've tried ~/.profile ~/.bash_profile. According to http://www.faqs.org/docs/bashman/bashref_63.html, I need to do export BASH_ENV=/etc/bashrc, but I don't know the bottom-most bash instance where I can export this.
Related
I've searched for an answer and read about different ways to do that in Mac but some of them are not relevant for Mojave or just didn't work for me.
I need to set Environment variable in terminal (bash), run script that creates processes, and I would like those processes to know the value of those environment variables.
How can I do that?
btw - writing export ENV_NAME=ENV_VAL in .bashrc or in .bash_profile didn't work.
Works for me. Have you RTFM? For example, ~/.bashrc is only read by interactive shells, not shell scripts. And ~/.bash_profile is only read by login shells. Again, shell scripts don't usually use the -l flag that would make them login shells. Also, if you put an export VAR=value statement in your ~/.bashrc it won't affect your current interactive shell. You need to start a new shell; e.g., by typing exec bash. Once you do that you should find the env var is defined. And it will be inherited by any process, including a shell script, you launch from that interactive session.
Note that if you run your script via crontab, for example, then you'll need a different means of setting the env var. For example, by using the --init-file flag or the BASH_ENV env var.
Imagine I am in a shell with the working directory set to a directory that contains an .envrc file. When I now open up a new tab in my terminal emulator, a new shell is launched into the same working directory as the original shell.
For some reason, the .envrc file is not loaded in this new shell. If manually exit the working directory and enter it again, the environment variables are then loaded.
Is there a way to make the variables load automatically when the shell is created?
I am running MacOS Mojave (10.14.4) with direnv 2.20.1, using bash as my shell. I am loading the direnv hooks through my .bashrc.
Under macOS Mojave (and probably older versions), saving a .bashrc has no effect. Instead, use a .bash_profile.
I found in my zsh environment I needed to put export in front of each variable in .envrc
VAR=x
doesn't work
Instead, use:
export VAR=x
Is there a way to make the variables load automatically when the shell is created
you can add this to your .bashrc
eval "$(direnv hook bash)"
it activate a hook on at the start of your bash.
If your bashrc is not loaded look at the bash_profile / bashrc to see which calls whom.
you can try to " source ~/.bashrc" to see if its your eval command that is not well written or your bashrc not called.
I am seeing a strange problem with the storing of an env in mac os.
I set custom env in ~/.bash_profile
export MYENV=user
Then ran the . ~/.bash_profile and then I printed the env using
printenv then I can see the MYENV=user in the list.
If I close the terminal and reopen and execute printenv then I could not see MYENV in the list still I can see the export MYENV=user in ~/.bash_profile. It seems strange to me.
I am using Mac os High Sierra 10.13.6.
Could some body please tell me what mistake I am doing?
Note that ~/.bash_profile is only run for login shells. From the man page:
When bash is invoked as an interactive login shell, or as a non-interactive shell
with the --login option, it first reads and executes commands from the file
/etc/profile, if that file exists. After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and exe-
cutes commands from the first one that exists and is readable. The --noprofile
option may be used when the shell is started to inhibit this behavior.
So if you terminal isn't launching the shell with -l, --login or with $0 having a leading hyphen it won't be a login shell and thus won't read ~/.bash_profile. You may need to reconfigure how your terminal launches the shell if you want the shell to read that config script.
On the other hand ~/.bashrc is always read by an interactive shell. So if you put the export in that script it should do what you expect. It certainly does for me. You replied to Amila that it didn't work for you. So I'd suggest a simple experiment. Open two terminal windows. In one edit ~/.bashrc and add these two lines:
echo running .bashrc
export WTF=abc
In the other window just run bash. It should echo that message and echo $WTF should print abc. Now open a new terminal window. If you don't see that message and the env var isn't present then something is inhibiting reading that config script. Possibly the shell is being run with the --norc flag.
~/.bash_profile is executed before the initial command prompt is returned to the user, which means after a new login. Try adding the environment variable to ~/.bashrc instead.
I am using Perlbrew in a brand new server (CentOs 6).
In my previous system (RedHat 4) WiTHOUT Perlbrew I used to use some aliases in the bash shell, like alias nreload='/usr/local/nginx/sbin/nginx -s reload' for example, to restart Nginx just putting the line in the .bash_profile file.
Now, with Perlbrew, I tried it in .bash_profile and in .bashrc, with no luck. I can make aliases on the fly, but I can't have them permanently available.
I followed the Perlbrew installation tips and copied the line source ~/perl5/perlbrew/etc/bashrc in the .bashrc file. I thought it should activate the Perlbrew Perl just by logging-in to the server, but it doesn't. So I must write source ~/perl5/perlbrew/etc/bashrc; everytime I need to use the Perlbrew Perl. It doesn't seems to be the right way.
Maybe the real problem is how I run Perlbrew. Maybe I do not understand the concept of Perlbrew opening another shell. If there is another shell, a subshell, how can I use alias in it?
Here is the content of my .bash_profile:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
source ~/perl5/perlbrew/etc/bashrc
export PATH
alias nreload='/usr/local/nginx/sbin/nginx -s reload'
BTW: the server is a remote one, and I control it from a local Macintosh, using Terminal. I have read something about Terminal is more a visual tool than a real Unix terminal, but I do not completely understand it.
I have found the way: aliases must be in .bashrc
I connect using (Macintosh) Terminal, and when logged to the online server I just "activate" a new shell typing "/bin/bash".
In this new shell I have the right Perlbrew Perl, an all the alias, because this new shell takes everything from the .bashrc file.
What confused me was that "a new shell" is not really "a new terminal window" as I was expecting, but a new "process", and this new process, obviously uses .bashrc as a start point.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What's the difference between .bashrc, .bash_profile, and .environment?
It seems that if I use
alias ls='ls -F'
inside of .bashrc on Mac OS X, then the newly created shell will not have that alias. I need to type bash again and that alias will be in effect.
And if I log into Linux on the hosting company, the .bashrc file has a comment line that says:
For non-login shell
and the .bash_profile file has a comment that says
for login shell
So where should aliases be written in? How come we separate the login shell and non-login shell?
Some webpage say use .bash_aliases, but it doesn't work on Mac OS X, it seems.
The reason you separate the login and non-login shell is because the .bashrc file is reloaded every time you start a new copy of Bash. The .profile file is loaded only when you either log in or use the appropriate flag to tell Bash to act as a login shell.
Personally,
I put my PATH setup into a .profile file (because I sometimes use other shells);
I put my Bash aliases and functions into my .bashrc file;
I put this
#!/bin/bash
#
# CRM .bash_profile Time-stamp: "2008-12-07 19:42"
#
# echo "Loading ${HOME}/.bash_profile"
source ~/.profile # get my PATH setup
source ~/.bashrc # get my Bash aliases
in my .bash_profile file.
Oh, and the reason you need to type bash again to get the new alias is that Bash loads your .bashrc file when it starts but it doesn't reload it unless you tell it to. You can reload the .bashrc file (and not need a second shell) by typing
source ~/.bashrc
which loads the .bashrc file as if you had typed the commands directly to Bash.
Check out http://mywiki.wooledge.org/DotFiles for an excellent resource on the topic aside from man bash.
Summary:
You only log in once, and that's when ~/.bash_profile or ~/.profile is read and executed. Since everything you run from your login shell inherits the login shell's environment, you should put all your environment variables in there. Like LESS, PATH, MANPATH, LC_*, ... For an example, see: My .profile
Once you log in, you can run several more shells. Imagine logging in, running X, and in X starting a few terminals with bash shells. That means your login shell started X, which inherited your login shell's environment variables, which started your terminals, which started your non-login bash shells. Your environment variables were passed along in the whole chain, so your non-login shells don't need to load them anymore. Non-login shells only execute ~/.bashrc, not /.profile or ~/.bash_profile, for this exact reason, so in there define everything that only applies to bash. That's functions, aliases, bash-only variables like HISTSIZE (this is not an environment variable, don't export it!), shell options with set and shopt, etc. For an example, see: My .bashrc
Now, as part of UNIX peculiarity, a login-shell does NOT execute ~/.bashrc but only ~/.profile or ~/.bash_profile, so you should source that one manually from the latter. You'll see me do that in my ~/.profile too: source ~/.bashrc.
From the bash manpage:
When bash is invoked as an
interactive login shell, or as a
non-interactive shell with the
--login option, it first reads and executes commands from the file
/etc/profile, if that file exists.
After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and
~/.profile, in that order, and reads
and executes commands from the first
one that exists and is readable. The
--noprofile option may be used when the shell is started to inhibit this
behavior.
When a login shell exits, bash
reads and executes commands from the
file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, bash
reads and executes commands from ~/.bashrc, if that file exists. This
may be inhibited by using the --norc option. The --rcfile file option
will force bash to read and execute commands from file instead of
~/.bashrc.
Thus, if you want to get the same behavior for both login shells and interactive non-login shells, you should put all of your commands in either .bashrc or .bash_profile, and then have the other file source the first one.
.bash_profile is loaded for a "login shell". I am not sure what that would be on OS X, but on Linux that is either X11 or a virtual terminal.
.bashrc is loaded every time you run Bash. That is where you should put stuff you want loaded whenever you open a new Terminal.app window.
I personally put everything in .bashrc so that I don't have to restart the application for changes to take effect.