Aliases and bash customization
OverviewTeaching: 10 minutes min
Exercises: 0 minQuestions
How do I customize my bash environment?Objectives
Add customizations to the
Change the prompt in a bash environment.
Bash allows us to customize our environments to fill our own particular needs.
Sometimes we need to use long commands that have to be typed over and
over again. Fortunately, the
alias command allows us to create
shortcuts for these long commands.
As an example, let’s create aliases for going up one, two, or three directories.
alias up='cd ..' alias upup='cd ../..' alias upupup='cd ../../..'
Let’s try these commands out.
cd /usr/local/bin upup pwd
We can also remove a shortcut with
If we create one of these aliases in a bash session, they will only last until the end of that session. Fortunately, bash allows us to specify customizations that will work whenever we begin a new bash session.
Bash customization files
Bash environments can be customized by adding commands to the
.bash_logout files in our home
.bashrc file is executed whenever entering
interactive non-login shells whereas
.bash_profile is executed for
login shells. If the
.bash_logout file exists, then it will be run
after exiting a shell session.
Let’s add the above commands to our
echo "alias up='cd ..'" >> ~/.bashrc tail -n 1 ~/.bashrc
alias up='cd ..'
We can execute the commands in
source ~/.bashrc cd /usr/local/bin up pwd
Having to add customizations to two files can be cumbersome. It we
would like to always use the customizations in our
then we can add the following lines to our
if [ -f $HOME/.bashrc ]; then source $HOME/.bashrc fi
Customizing your prompt
We can also customize our bash prompt by setting the
variable. To set our prompt to be
$ , then we can run the command
export PS1="$ "
To set the prompt to
$ for all bash sessions, add this line to the
Further bash prompt
are possible. To have our prompt be
we would set
export PS1="\u@\h[\W]: "
\u represents username,
\h represents hostname, and
represents the current directory.
Aliases are used to create shortcuts or abbreviations
.bash_profilefiles allow us to customize our bash environment.
PS1system variable can be changed to customize your bash prompt.