Customizing bash with configuration files
Most commands you type on the command line can be placed in a special file, to be evaluated when you log in or start a new bash session. It's common to customize your shell by putting function definitions, aliases, and environment variable settings in one of these files.
Common commands to put into a configuration file include the following:
# Define my colors for ls LS_COLORS='no=00:di=01;46:ln=00;36:pi=40;33:so=00;35:bd=40;33;01' export LS_COLORS # My primary prompt PS1='Hello $USER'; export PS1 # Applications I install outside the normal distro paths PATH=$PATH:/opt/MySpecialApplication/bin; export PATH # Shorthand for commands I use frequently function lc () {/bin/ls -C $* ; }
What customization file should I use?
Linux and Unix have several files that might hold customization scripts. These configuration files are divided into three camps—those sourced on login, those evaluated when an interactive shell is invoked, and files evaluated whenever a shell is invoked to process a script file.
How to do it...
These files are evaluated when a user logs into a shell:
/etc/profile, $HOME/.profile, $HOME/.bash_login, $HOME/.bash_profile /
Note
Note that /etc/profile
, $HOME/.profile
and $HOME/.bash_profile
may not be sourced if you log in via a graphical login manager. That's because the graphical window manager doesn't start a shell. When you open a terminal window, a shell is created, but it's not a login shell.
If a .bash_profile
or .bash_login
file is present, a .profile
file will not be read.
These files will be read by an interactive shell such as a X11 terminal session or using ssh
to run a single command like: ssh 192.168.1.1 ls /tmp
.
/etc/bash.bashrc $HOME/.bashrc
Run a shell script like this:
$> cat myscript.sh #!/bin/bash echo "Running"
None of these files will be sourced unless you have defined the BASH_ENV
environment variable:
$> export BASH_ENV=~/.bashrc $> ./myscript.sh
Use ssh
to run a single command, as with the following:
ssh 192.168.1.100 ls /tmp
This will start a bash shell which will evaluate /etc/bash.bashrc
and $HOME/.bashrc
, but not /etc/profile
or .profile
.
Invoke a ssh login session, like this:
ssh 192.168.1.100
This creates a new login bash shell, which will evaluate the following:
/etc/profile /etc/bash.bashrc $HOME/.profile or .bashrc_profile
Note
DANGER
: Other shells, such as the traditional Bourne shell, ash, dash, and ksh, also read this file. Linear arrays (lists) and associative arrays, are not supported in all shells. Avoid using these in /etc/profile
or $HOME/.profile
.
Use these files to define non-exported items such as aliases desired by all users. Consider this example:
alias l "ls -l" /etc/bash.bashrc /etc/bashrc
Use these files to hold personal settings. They are useful for setting paths that must be inherited by other bash instances. They might include lines like these:
CLASSPATH=$CLASSPATH:$HOME/MyJavaProject; export CLASSPATH $HOME/.bash_login $HOME/.bash_profile $HOME/.profile
Note
If .bash_login
or .bash_profile
are present, .profile
will not be read. A .profile
file may be read by other shells.
Use these files to hold your personal values that need to be defined whenever a new shell is created. Define aliases and functions here if you want them available in an X11 terminal session:
$HOME/.bashrc, /etc/bash.bashrc
Note
Exported variables and functions are propagated to subordinate shells, but aliases are not. You must define BASH_ENV
to be the .bashrc
or .profile
, where aliases are defined in order to use them in a shell script.
This file is evaluated when a user logs out of a session:
$HOME/.bash_logout
For example, if the user logs in remotely they should clear the screen when they log out.
$> cat ~/.bash_logout # Clear the screen after a remote login/logout. clear