Customize Environment
Linux Commands - Shells

Customize the User Environment

Users are able to customize their environment because the shell provides the tools to do so.
The environment is comprised of a series of settings that provide a look and feel that the user is comfortable with or that the corporation deems necessary to create a productive work setting. Bash provides systematic ways to setup an environment that will meet your needs to help you function more effectively.

Lesson 9 / Lesson 11

The login shell has the responsibility to set up the environmental variables that have been provided by the system or set specifically be the administrator.

The Linux operating system provides system-wide parameters for the user in

There are also parameters that are set up for users in the user home directory:

If these files do not exist, automatically the system uses the default /etc/profile for users. 

The .bashrc file is run if you have already logged in and create a new shell.  Hence, you can separate commands used with the login (.bash_profile) and the subsequent shells (.bashrc).

The .bash_profile script that is the default is listed below.

PS1='\h:\w\$ '
PS2='> '
export EDITOR

You may also see .bash_history which keeps a list of the commands that the user has executed.

Any changes you make to this script must be saved and then you must logout and login again. However it is possible to use the source command which will run all of the commands that are in the script:

source .bash_profile

Now there is a catch, bash provides a way for a system to use two alternative files other than .bash_profile, these files are .bash_login or .profile. If either of these files is listed and .bash_profile is not then they will run, but if .bash-profile is listed it will run.

The .bash_profile is read by the system and executes any command located there only when a user logs into the system, and not when a user starts a new shell. When the user starts a new shell .bashrc file is read. This setup allows the user to separate the commands needed at startup and those that may be important when starting a subshell.

The .bash_logout provides a way to execute commands when the user logs out of the system. One useful application for this in a user's home directory is that an administrator can provide a way to kill all of the user's applications when logging out just in case an application was not closed correctly or that hangs.

Here is an example of a simple command that copies all OpenOffice Writer files to a USB device in a directory called MyDocs when the user logs out.

Create a file named .bash_logout in the users home directory if it has not been created already. In that file use this command in a line and save the file:

cp Documents/*.odt /media/usb/MyDocs

Note the case and that since the command will be issued from the user's home directory the path is simply the Documents directory. Of course this command will require the user to save all the Writer files they want to copy into the Documents directory or they will not be copied. You will also need a directory called MyDocs in the USB device.

Shell Aliases

The alias feature in the bash shell allows you to create a different name for a command or script that you would like to make easier to use. The format for creating an alias is:

alias name=command/script

Now if you were going to use an alias for a command that involved a long string of characters because of a long pathname or command you would need to enclose the command in a single quotes, like this for a backup command:

alias bk='cp /home/fred/Documents/*.odt  /media/usb*/MyDocs'

Instead of having to type this whole sting the user can just use the command:

Notice in this alias there are special wildcard characters like * that will be interpreted correctly by bash to function like they are supposed to function.

One issue that takes a lot of typing is when  you login to SSH accounts that are on different port numbers.  Not only do you have to remember the port number but also user and keep it separate from the other servers you manage.  You can create aliases for each of the servers.

alias sb='ssh -p 4202 This e-mail address is being protected from spambots. You need JavaScript enabled to view it '
alias sc='ssh -p 8702 This e-mail address is being protected from spambots. You need JavaScript enabled to view it '
alias sd='ssh -p 4508 This e-mail address is being protected from spambots. You need JavaScript enabled to view it '

Aliases are also recursive in that you will be able to use the alias with other aliases or in scripts.

You can see a list of aliases that bash currently uses by typing:



Copyright CyberMontana Inc. and
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874