Linux/Unix – Customizing Future Shells

 

There are a number of configuration startup files in your home directory that you can edit to make your configurations permanent. You can also edit these files to specify commands to be run whenever you first log in, log out, or start a new shell. These configuration files are text files that can be edited with any text editor.

When you log in, bash first checks to see if the file `/etc/profile’ exists, and if so, it executes the commands in this file. This is a generic, system−wide startup file that is run for all users; only the system administrator can add or delete commands to this file.

Next, bash reads and executes the commands in `.bash_profile’, a “hidden” file in your home directory. Thus, to make a command run every time you log in, add the command to this file. For all new shells after you’ve logged in (that is, all but the “login shell”), bash reads and executes the commands in the `.bashrc’ file in your home directory. Commands in this file run whenever a new shell is started except for the login shell.

There are separate configuration files for login and all other shells so that you can put specific customizations in your `.bash_profile’ that only run when you first log in to the system. To avoid having to put commands in both files when you want to run the same ones for all shells, append the following to the end of your `.bash_profile’ file:

if [ −f ~/.bashrc ]; then . ~/.bashrc; fi

 

This makes bash run the `.bashrc’ file in your home directory when you log in. In this way, you can put all of your customizations in your `.bashrc’ file, and they will be run both at log in and for all subsequent shells. Any customizations before this line in `.bash_profile’ run only when you log in. For example, a simple `.bash_profile’ might look like this:

 

# Comment” lines in shell scripts begin with a # character.

# They are not executed by bash, but exist so that you may document your file.

# You can insert blank lines in your file to increase readability; bash will not mind.

# Generate a welcome message when you log in.

 

figlet ‘Good day, ‘$USER’!’

# Now run the commands in .bashrc

if [ −f ~/.bashrc ]; then . ~/.bashrc; fi

 

This `.bash_profile’ prints a welcome message with the figlet text font tool and then runs the commands in the `.bashrc’ file. A simple .bashrc file might look like this:

 

# Make color directory listings the default.

alias ls=”ls −−color=auto”

# Make “l” give a verbose directory listing.

alias l=”ls −l”

# Set a custom path.

PATH=”/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:~/bin:.”

# Set a custom shell prompt.

PS1=”[\w] $ “

# Make a long history list and history file.

HISTSIZE=20000

HISTFILESIZE=20000

# Export the path and prompt variables for all variables you define.

export HISTSIZE HISTFILESIZE PATH PS1

 

This `.bashrc’ sets a few useful command aliases and uses a custom path and shell prompt whenever a new shell is run; with the preceding `.bash_profile’, this `.bashrc’ is also run at login. When you log out, bash reads and executes the commands in the `.bash_logout’ file in your home directory, if it exists. To run commands when you log out, put them in this file.  To clear the screen every time you log out, your `.bash_logout’ would contain the following line:

clear

This executes the clear command, which clears the screen of the current terminal, such as in the xterm window where you type it, or in a virtual console.

 

Advertisements

About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on March 15, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: