BASH Configuration
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 known as 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 accounts settings page.
By customizing your shell you can take advantage of the command prompt and be more productive.
Which Files to Edit
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 and type
source ~/.bashrc
.- If your .bashrc file is not in the root of your home directory, you may need to modify this command.
.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: This file contains a list of all the commands you have entered. It is written to every time you exit the shell and does not need to be edited.
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:
username@machine:~ $ env HOSTNAME=machine PVM_RSH=/usr/bin/rsh TERM=xterm SHELL=/bin/bash HISTSIZE=1000
If you want to see what has been set for a certain variable, for example HOME, type echo $HOME
:
username@machine:~ $ echo $HOME /users/guest/u/username
This is where your home directory is located.
You can also set custom variables:
username@machine:~ $ BOB=hello username@machine:~ $ echo $BOB hello
If you put export in front of the variable definition, all other terminals will recognize it:
username@machine:~ $ export BOB=hello In new terminal: username@machine:~ $ 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:
username@machine:~ $ echo $PATH /bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/X11R6/bin:/usr/java/jdk1.3.1_01/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.
You'll want to add a line in your .bash_profile that looks something like this:
export PATH="$PATH:/usr/java/jdk1.3.1_01/bin:."
This will take the system wide PATH and then add on other directories that you want (for example, /usr/java/jdk1.3.1_01/bin).
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, to ssh into a CS lab machine, I can do the following to avoid having to type out the entire command every time:
username@machine:~ $ alias labmachine='ssh username@labmachine.cs.byu.edu' username@machine:~ $ labmachine username@labmachine.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 other examples of aliases you can set:
alias ll='ls -l' alias c='clear'
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:
username@machine Thu Apr 10 ~/ $ PS1='\u@\h \d $ ' username@machine Thu Apr 10 $ PS1='\u@\h $ ' username@machine $ PS1='\u $ ' username $ PS1=' $ ' $ PS1='\u@\h \d \w $ ' username@machine Thu Apr 10 ~/ $
Example config files
.bashrc: # .bashrc # Set Prompt export PS1='\[\033[01;32m\]\u@\h \[\033[01;34m\]\W \$ \[\033[00m\]' # Aliases alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' alias c='clear' alias ll='ls -lh' # Source Global Definitions File if [ -f /etc/bashrc ]; then . /etc/bashrc fi # Sets the Number of Lines in .bash_history export HISTSIZE=10000 .bash_profile: # .bash_profile # Get Aliases and Functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi