I frequently edited the .bashrc
file to export new environment variables.
Rather than close the console and start a new one to refresh the env variables, is there a convenient way to refresh?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityWithin the same window, you can simply type bash
to start a new one. This is equivalent to closing the window and re-opening a new one.
Alternatively, you can type source ~/.bashrc
to source the .bashrc
file.
source
. you can just type dot instead of it: . ~/.bashrc
.
source
shows the user which command is called. I don't believe in showing the shorthanded commands to new users until they understand what the code is doing.
source
is a bashism that is longer to type yet offers no advantages over its portable across all shells equivalent, .
, I don't see the point of encouraging anyone, new or experienced, to use it.
Just use
source ~/.bashrc
or
. ~/.bashrc
In addition to what others have suggested, I have found out that source
won't unset the previously assigned environment variables. So, if you want to unset environment variables, you have to do it manually using unset <var>
.
Adding to another answer, I find it's helpful to define the following alias:
alias refreshenv="bash;exit"
Doing this will ensure that the parent bash instance will be killed as soon as you exit the child bash instance and so on. Avoids you having to type exit
multiple times, as you'd have to do with just typing bash
.
Since this question comes up on Google when you search for how to reload the environment inside a shell script, here's one:
Spawn a login shell with an empty environment and then examine its state:
eval "$(exec /usr/bin/env -i "${SHELL}" -l -c "export")"
Note that this will not consider non-exported variables. I would suggest set -o posix && set
for that instead, but it has the problem that it can break, as you can't just write every variable in Bash. Parsing its output is not easy either, so I wouldn't recommend it. It's unlikely it's what you want though.
.profile
, not.bashrc
. The.profile
is sourced when you login (e.g. via a display manager) and also takes effect for programs that aren't started from your shell (e.g. via your desktop environment). This makes it, AFAICT, basically impossible to modify it without logging out though. Then only solution to change the environment after login I'm aware of is the fish shell with their universal variables. This shell is not well supported in display managers though (yet?).