How do I create a permanent Bash alias?
To create an alias permanently add the alias to your .bashrc
file
gedit ~/.bashrc
And then add your alias at the bottom.
Now execute . ~/.bashrc
in your terminal (there should be a space between the .
and ~/.bashrc
.
Now you can check your alias.
There are a lot of ways to create an alias. The most used ways are:
Add aliases directly in your
~/.bashrc
fileFor example: append these line to
~/.bashrc
filealias ll='ls -l' alias rm='rm -i'
Next time (after you have logged out/in, or done
. ~/.bashrc
) when you typerm
therm -i
command will be executed.The second method lets you make a separate aliases file, so you won't have to put them in
.bashrc
, but to a file of your choice. First, edit your~/.bashrc
file and add the following lines if they don't already exist, or uncomment them if they do:if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
Save it and close the file. After that, all you have to do is create a
~/.bash_aliases
file and add your aliases there, with the same format specified in the first method.Contents of my
~/.bash_aliases
file:alias cs='cd;ls'
It sounds to me like your only problem is simply trying to execute .bashrc when it is not executable. But this isn't the correct way to do it; whenever you make a change to this file, you should "execute" it by the command:
source ~/.bashrc
Otherwise, it will simply create a new shell, execute the file in the new shell's environment, then discard that environment when it exits, thereby losing your change. By sourcing the script, it executes within the current shell, so it will remain in effect.
I'm assuming the second error was because bash_aliases does not exist. It is not required, just recommended to keep your changes separate and organized. It is only used if it exists, and you can see the test for it in .bashrc:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
This says that if the file ~/.bash_aliases exists, then run it.