Customize the bash shell environments
- Strictly speaking there are two types of shell variables:
- Local variables (shell variable) - Used by shell and or user scripts. All user created variables are local unless exported using the export command.
- Environment variables - Used by shell or user but they are also passed onto other command. Environment variables are passed to subprocesses or subshells.
How do I configure and customize the Bash shell environment?
- Your Bash shell can be configured using the following:
What should I put in shell starup files for customization?
A typically Linux or UNIX user do the following:
- Setup a custom prompt.
- Setup terminal settings depending on which terminal you're using.
- Set the search path such as JAVA_HOME, and ORACLE_HOME.
- Set environment variables as needed by programs.
- Run commands that you want to run whenever you log in or log out.
How do I view local variables?
Use the set built-in command to view all variables:
Usually, all upper-case variables are set by bash. For example,
echo $SHELL echo $MAIL
How do I export local variables?
Use the export command:
export EDITOR=/usr/bin/vim # export DISPLAY environment variable and run xeyes export DISPLAY=localhost:11.0 xeyes
Be careful when changing the shell variables. For a complete list of variables set by shell, read the man page for bash by typing the following command:
How do I view environment variables?
Use the env command to view all environment variables:
ORBIT_SOCKETDIR=/tmp/orbit-vivek SSH_AGENT_PID=4296 GPG_AGENT_INFO=/tmp/gpg-ElCDl5/S.gpg-agent:4297:1 TERM=xterm SHELL=/bin/bash XDG_SESSION_COOKIE=186611583e30fed08439ca0047067c9d-1255929792.297209-1700262470 GTK_RC_FILES=/etc/gtk/gtkrc:/home/vivek/.gtkrc-1.2-gnome2 WINDOWID=48252673 GTK_MODULES=canberra-gtk-module USER=vivek SSH_AUTH_SOCK=/tmp/keyring-s4fcR1/socket.ssh GNOME_KEYRING_SOCKET=/tmp/keyring-s4fcR1/socket SESSION_MANAGER=local/vivek-desktop:/tmp/.ICE-unix/4109 USERNAME=vivek DESKTOP_SESSION=gnome PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games GDM_XSERVER_LOCATION=local PWD=/home/vivek LANG=en_IN GDM_LANG=en_IN GDMSESSION=gnome SHLVL=1 HOME=/home/vivek GNOME_DESKTOP_SESSION_ID=this-is-deprecated LOGNAME=vivek DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-16XVNAMkFB,guid=0acb6a08e3992ccc7338726c4adbf7c3 XDG_DATA_DIRS=/usr/local/share/:/usr/share/:/usr/share/gdm/ WINDOWPATH=7 DISPLAY=:0.0 COLORTERM=gnome-terminal XAUTHORITY=/home/vivek/.Xauthority OLDPWD=/usr/share/man _=/usr/bin/env
Common Environment Variables
- HOME - Your home directory path.
- PATH - Set your executable search path.
- PWD - Your current working directory.
- See more standard environment variables list.
How do I locate command?
The which command displays the pathnames of the files which would be executed in the current environment. It does this by searching the PATH for executable files matching the names of the arguments.
Show fortune command path which print a random, hopefully interesting, adage on screen. Type the following command:
Display your current PATH:
Customize your PATH variable and remove /usr/games from PATH:
Now, try searching fortune command path, enter:
Try executing fortune command:
-bash: fortune: command not found
The fortune command could not be located because '/usr/games' is not included in the PATH environment variable. You can type full command path (/usr/games/fortune) or simply add /usr/games to PATH variable:
export PATH=$PATH:/usr/games fortune
Your lucky number has been disconnected.
The whereis command is used to locate the binary, source, and manual page files for a command.
whereis command-name whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz
The whatis command is used display a short description about command. whatis command searches the manual page names and displays the manual page descriptions for a command:
whatis command-name whatis date whatis ifconfig whatis ping
date (1) - print or set the system date and time ifconfig (8) - configure a network interface ping (8) - send ICMP ECHO_REQUEST to network hosts