Part of the reason Linux is so powerful is because of the ability to do everything from the command line. The command line, more properly know a shell, allows you to navigate the file system and run programs.

There are many shells available under Linux, such as BASH, SH, CSH, TCSH, etc. The default on most linux distributions is BASH. ZSH is also worth taking a look at.

You can change your default shell for the Linux Open Labs from the account edit webpage

By customizing your shell you can take advantage of the command prompt and be more productive.

Which files to Edit

The main system wide bash configuration is located in /etc/profile, /etc/profile.d, and /etc/bashrc. You might want to look there to see was has been set, and then edit your personal "dot" files in your home directory.

The dot files are configuration files located in your home directory (they start with a '.', hence the name dot files). If you type ls in your home directory it will only show you your normal files; if you type in ls -a it will show both normal and dot files.

If you edit one of these files you'll have to do one of the following before your changes take effect:

  • If you edit your .bash_profile, log out and log back in; if you edit your .bashrc open up a new terminal.
  • Type source <filename> for example: source .bashrc
.bash_profile:This file contains a list of commands to be executed when you log in (i.e. read once at the beginning of the session).
.bashrc:This file contains a list of commands to be executed every time you open a new shell (or terminal).
.bash_logout:This file contains a list of commands to be executed when you leave the shell.
.bash_history:Actually, you don't need to edit this file (this is being mentioned just for your information). This file contains a list of all the commands you have entered. It is written to every time you exit the shell.

What to Configure

Environmental Variables

There are many global variables that are used by the shell and other programs. You can see what has been set by typing env . You will see something similar to the following:

[emcnabb@grape emcnabb]$ env
HOSTNAME=grape
PVM_RSH=/usr/bin/rsh
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000

If you can see what has been set for a certain variable, for example HOME, by typing echo $HOME:

[emcnabb@grape emcnabb]$ echo $HOME
/users/admin/emcnabb

This is where my home directory is located.

You can also set custom variables:

[emcnabb@grape emcnabb]$ BOB=hello
[emcnabb@grape emcnabb]$ echo $BOB
hello

If you put export in front of the variable definition, all other terminals will recognize it:

[emcnabb@grape emcnabb]$ export BOB=hello
[emcnabb@grape emcnabb]$ echo $BOB
hello

Any variables you set from the command line will last only for the duration of that instance of the shell. If you want to have a variable set every time you start up a terminal/shell, add exactly what you typed to your .bashrc file.

PATH

The PATH variable is a list of directories that the shell looks in when a command is run. For example, the directory list command ls is located in /bin/ls. How does your shell know what command to run when you type ls, It looks in your PATH variable. Try the following:

[emcnabb@grape emcnabb]$ echo $PATH
/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/X11R6/bin:/usr/java/jdk1.3.1_01/bin:/users/ta/cs324ta/bin:.

As you can see, /bin is one of the directories that is searched. The PATH is read from left to right, so if there is an ls command located in /bin and /usr/bin, the /bin one will be executed. I have customized my PATH variable to include the /users/ta/cs324ta/bin directory so that I don't have to type "/users/ta/cs324ta/bin/myexecutable" every time I want to run the TA's example program, just "myexecutable".

You'll want a line in your .bash_profile that looks something like this:

export PATH="$PATH:/usr/java/jdk1.3.1_01/bin:/users/admin/cs324ta/bin:."

This will take the system wide PATH and then add on other directories that you want (for example /users/admin/cs324ta/bin).

CLASSPATH

The CLASSPATH is an environmental variable that tells the Java compiler where to look for class files to import or where to find class files to interpret. If you do medium to large Java programming projects you'll probably want to edit this variable. You can set this just like you do your PATH variable. Here is what my CLASSPATH variable is set to:

export CLASSPATH="/opt/jakarta/tomcat/common/lib/servlet.jar:/opt/sun-j2ee-1.3.1/lib/j2ee.jar:/home/emcnabb/postgresql.jar:."

aliases

You probably have some long commands that you type in quite often. In order to save time, you can create aliases. The basic structure is alias <myalias>='<command>'

For example, on my home computer I ssh into grape, but don't want to type in the whole command each time. I can do the following:

bash-2.05b$ alias grape='ssh emcnabb@grape.cs.byu.edu'
bash-2.05b$ grape
emcnabb@grape.cs.byu.edu's password:

If you want to have that alias set for all of your terminals, add it to your .bashrc file. The following are several other examples of what I have in my .bashrc:

alias ll='ls -l'
alias c='clear'
alias p='pine'
alias temp='cd $HOME/tmp' #Go to my tmp directory

Command Prompt

You can change the text that is displayed while the shell is waiting for input. This configuration is stored in the variable PS1 where PS1='value'.

The following are different values that can be set:

  • \a an ASCII bell character (07)
  • \d the date in "Weekday Month Date" format (e.g., "Tue May 26")
  • \e an ASCII escape character (033)
  • \h the hostname up to the first '.'
  • \H the hostname
  • \j the number of jobs currently managed by the shell
  • \l the basename of the shell's terminal device name
  • \n newline
  • \r carriage return
  • \s the name of the shell, the basename of $0 (the portion following the final slash)
  • \t the current time in 24-hour HH:MM:SS format
  • \T the current time in 12-hour HH:MM:SS format
  • \@ the current time in 12-hour am/pm format
  • \u the username of the current user
  • \v the version of bash (e.g., 2.00)
  • \V the release of bash, version + patchlevel (e.g., 2.00.0)
  • \w the current working directory
  • \W the basename of the current working directory
  • \! the history number of this command
  • \# the command number of this command
  • \$ if the effective UID is 0, a #, otherwise a $
  • \nnn the character corresponding to the octal number nnn
  • \\ a backslash
  • \[ begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt
  • \] end a sequence of non-printing characters

Here is an example of me playing with the PS1 variable. Notice how the command prompt changes as PS1 changes:

emcnabb@medusa Thu Apr 10 ~/SANDBOX/PHP $  PS1='\u@\h \d $ '
emcnabb@medusa Thu Apr 10 $  PS1='\u@\h $ '
emcnabb@medusa $  PS1='\u $ '
emcnabb $  PS1=' $ '
 $ PS1='\u@\h \d \w $ '
emcnabb@medusa Thu Apr 10 ~/SANDBOX/PHP $

The PS1 value that I personally use is:

PS1='[ \e[31m\u@\h:\e[34m\w \e[m] '

Example config files

  • .bashrc:

    #.bashrc
    #set my prompt
    export PS1='\[\033[01;32m\]\u@\h \[\033[01;34m\]\W \$ \[\033[00m\]'
    #append my binary directories to my path
    export PATH=$PATH:$HOME/bin:$HOME/java_bin
    #set aliases that I like
    alias rm='rm -i'
    alias cp='cp -i'
    alias mv='mv -i'
    alias cls='clear'
    alias ll='ls -lh'
    alias xterm='xterm -fg white -bg black -j +sb -fs 7 -b 0 -geometry 100x75'
    # Source global definitions file
    if [ -f /etc/bashrc ]; then
     . /etc/bashrc
    fi
    #set environmental variables
    mesg y
    #receive messages with the write command
    export HISTSIZE=10000
    #sets the number of lines in my console history
    export CVSROOT=emcnabb@cvs:/mycvs #set my cvs repository location
    export CVS_RSH=ssh #set my cvs repository protocol
    
  • .bash_profile:

    # .bash_profile
    #set keyboard mapping to dvorak
    #Only want to set keyboard if X is running.
    #setxkbmap throws an error otherwise.
    if [ "$DISPLAY" != "" ]; then
    setxkbmap dvorak
    fi
    # Get the aliases and functions
    if [ -f ~/.bashrc ]; then
    . ~/.bashrc
    fi