I'm trying to make a bash alias for traversing through a few folders, but the alias does not save after I close terminal.
I've already saved the alias in the .bashsrc file and have also have run the command . ~/.bashsrc.
Here's what I've done:
sudo nano .bashsrc
Inside bashsrc:
alias x = 'cd Documents/Photos/Family'
And then saved and exit.
. ~/.bashsrc
The alias works in that terminal window, but shows "command not found" if I restart terminal or open a new tab.
You should prefer setting your changes in ~/.bashrc
and ~/.bash_profile
.
alias x='cd Documents/Photos/Family'
Also, remember aliases won’t be exported to subshells and while using this alias you always need to be present in the directory where Documents
is present. I guess your Documents
directory is present in home, so it would be better if you do something like:
alias x="cd "$HOME"/Documents/Photos/Family"
Just add this line to the end of both the files and save.
In simple language there are two types of shells for your case, login and non-login interactive shell.
Before starting of a login shell, .bash_profile
is sourced and before starting of a non-login shell .bashrc
is sourced.
So you should add your changes in both.
If you are on macOS, .bashrc
is next to useless for your case. MacOS would treat every shell you open in terminal as a login shell. Although there are some exceptions which i don’t remember.
As mentioned by David in the comments, in some distros ~/.bash_profile
is just ~/.profile
(e.g. Debian, openSUSE, etc).