I am ssh-ing into a linux environment with bash cli, and started looking into .bashrc
stuff to add my own aliases for convenience, on the linux side. I ended up with my home directory looking something like this:
.bash_aliases
.bash_profile
.bashrc
And I essentially call source .bash_aliases
in bashrc, and I call source .bashrc
in bash_profile. Is this a redundant flow? Should I just call both in bash_profile? Or should I just remove the bash_aliases file and put it all in bashrc?
Also, do I need the .bash_profile file if I'm ssh-ing into the linux environment every time? I thought that having the .bashrc file was enough to make the aliases persistent.
I was looking into answers here that explained about .bash_profile
a bit: Should aliases go in .bashrc or .bash_profile? but I still don't know if it's necessary or not.
Lastly, I've seen a few ways to call source .bashrc
. Currently, I am using Way #1. Is there any reason for/against these? Are there any real differences or is it just different ways to write the same code? And what does "source" mean?
Way #1:
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Way #2: (this is just a shortened way #1, right?)
[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"
Way #3:
test -s ~/.bashrc && . ~/.bashrc || true
Way #4:
if [ -e $HOME/.bashrc ]; then
source $HOME/.bashrc
fi
Way #5: (no check)
source $HOME/.bashrc
And I essentially call
source .bash_aliases
in bashrc, and I callsource .bashrc
in bash_profile. Is this a redundant flow?
No. When bash starts as an interactive login shell, it reads ~/.bash_profile
, if that exists, but it does not automatically read ~/.bashrc
. When bash starts as an interactive shell that is not a login shell, it reads ~/.bashrc
, if that exists, but it does not automatically read ~/.bash_profile
.
It is common for there to be things you want run at startup of every interactive shell. To get them in both login and non-login shells without duplication, one typically would put them in .bashrc
and have .bash_profile
read .bashrc
(if it exists). This is a common pattern, and the only good reason not to do it would be that there are things you want run only in interactive non-login shells, not interactive login shells. That would be very unusual.
Each process, including shells, inherits environment variables from its parent, so arguably, you avoid redundancy by setting environment variables directly in .bash_profile
, not in .bashrc
. And that makes an observable difference if you use variables' original values to set new ones, as is common for PATH
.
On the other hand, aliases are not inherited, so you need to set the ones you want separately in each shell. That is well served by having .bashrc
set them up.
Your .bash_aliases
is of course your own invention. I don't see a particular advantage to separating out aliases into a separate file, but I don't think it's harmful, and it doesn't affect the above redundancy analysis.
Should I just call both in bash_profile?
Probably not.
Or should I just remove the bash_aliases file and put it all in bashrc?
That would be more typical. But I'm uncertain what advantages you see in putting the aliases in a separate file, so I can't speak to how significant those may be. I see only minor problems with putting the aliases in their own file, mainly the (slightly) increased complexity and the bespoke nature. These aren't compelling reasons to change what you're already doing.
Also, do I need the .bash_profile file if I'm ssh-ing into the linux environment every time? I thought that having the .bashrc file was enough to make the aliases persistent.
Every SSH login that gets you a shell gets you a login shell. As described already, login shells do not automatically read .bashrc
. So to a first approximation, yes, you do need .bash_profile
, or one of the alternatives that Bash login shells will read if ~/.bash_profile
is not present.
I was looking into answers here that explained about
.bash_profile
a bit: Should aliases go in .bashrc or .bash_profile? but I still don't know if it's necessary or not.
Aliases that you want to have in every non-login shell should be set via .bashrc
. Setting them in .bash_profile
is not effective for that. You may also set aliases independently in .bash_profile
, but it is usually better to avoid duplication by having .bash_profile
read .bashrc
.
Lastly, I've seen a few ways to call source
.bashrc
.
You want two things here:
.bashrc
if possible.bashrc
does not exist or cannot be readYou can use test
or [
or [[ ... ]]
to evaluate conditions, and you can use if
or ||
or &&
for branching based on the result, or on whether a command exits with a failure status. How you put those together and exactly what condition you test is largely a matter of preference. I suggest not failing to test at all. My personal preference is
test -r ~/.bashrc && . ~/.bashrc
(so, way #6).