Fall 2015 - September to December 2015 - Updated 2017-02-06 02:44 EST
This material is optional.
Shells have a number of options and settings that make doing system administration work easier. You can also define aliases and functions to create your own command names or set common options on commands that are useful to system administrators.
The settings described in this file are useful for systems administrators, but they can be confusing if you don’t know what they mean or how they work.
If you don’t know the meaning of a setting, don’t use it. You can RTFM in the bash
man page for all BASH settings.
None of the settings in this file are needed for this course. Everything here is optional. Use it if you find it useful.
Tests and exams in this course presume no options or aliases. Do not answer questions on tests based on these options or aliases. Always presume that you have no aliases and that the shell is running with the default options.
A common mistake is to set
failglob
and/ornoclobber
and then get different error messages than the ones expected in tests and exams, where these options are not enabled. Don’t do that.
Do not set any options that you do not understand!
Below are described several BASH shell options for a sysadmin that you might put in the interactive section of your .bashrc
(below the exit if not interactive
line):
shopt -s failglob
set -o ignoreeof
set -o noclobber
set -o notify
set -o nounset
set -o physical
set -o interactive-comments
Here are some quick explanations (RTFM man bash
for details):
shopt -s failglob
IndexGive an error message if a GLOB fails to match any pathnames, instead of silently returning the GLOB pattern unchanged. (If a sysadmin uses an unquoted GLOB pattern, s/he expects it to match something. If s/he doesn’t want the shell to see the GLOB characters, s/he puts quotes around the string.)
set -o ignoreeof
IndexThe shell will ignore ^D
(EOF) at the prompt; type exit
to exit.
set -o noclobber
IndexProtects files from being overwritten by the shell when shell output >file
redirection is used. As a sysadmin, I almost never use redirection to overwrite an existing file, so turning on this option prevents me from doing so accidentally:
$ rm -f foo
$ date >foo # creates new file
$ date >foo # silently overwrites existing file
$ ls >foo # silently overwrites existing file
$ who >foo # silently overwrites existing file
$ set -o noclobber
$ date >foo # does not overwrite
bash: foo: cannot overwrite existing file
$ ls >foo # does not overwrite
bash: foo: cannot overwrite existing file
$ who >foo # does not overwrite
bash: foo: cannot overwrite existing file
With this option enabled, you must use the “force” syntax >|file
(that’s a pipe character) to force overwriting an existing file using shell output redirection, e.g.
$ set -o noclobber
$ date >foo # does not overwrite
bash: foo: cannot overwrite existing file
$ date >| foo # FORCED overwrite of existing file
set -o notify
IndexNotifies user immediately when a background job (started with &
or bg
) completes. Without this, you won’t be told that the background job is done until you push an ENTER
.
set -o nounset
IndexDisplays an error when expanding a variable that has not been set. Essential for detecting typing errors in variable names! Without this shell option set, your variable name typing mistakes go unnoticed and silently result in empty expansions:
$ foo=/tmp/somewhere
$ ln -s /etc/passwd $goo # typing error silently does empty expansion
$ ls -l /tmp/somewhere # desired symlink was not created
ls: cannot access /tmp/somewhere: No such file or directory
$ ls -l passwd # created wrong symlink name in current dir
lrwxrwxrwx 1 me me 11 Jan 1 1:00 passwd -> /etc/passwd
With this option set, your variable name typing mistakes are immediately obvious:
$ set -o nounset
$ foo=/tmp/somewhere
$ ln -s /etc/passwd $goo # typing error causes shell error now
bash: goo: unbound variable
$ ln -s /etc/passwd $foo # fix the typing mistake
$ ls -l /tmp/somewhere
lrwxrwxrwx 1 me me 11 Jan 1 1:01 /tmp/somewhere -> /etc/passwd
set -o physical
IndexExpand all symbolic links in the output of pwd
and when using cd
so that you see and use the real physical directory names and paths. Essential for system administrators to know exactly where you are in the file system. Without -o physical
, your real current working directory is hidden from you across symbolic links:
$ set +o physical # turn OFF physical (NOT RECOMMENDED)
$ cd /tmp
$ ln -s -f /var/lib/vim/addons foo ; ls -l foo
lrwxrwxrwx 1 xxx xxx 19 Mar 16 15:17 foo -> /var/lib/vim/addons
$ cd foo ; pwd ; /bin/pwd # use built-in pwd and external pwd
/tmp/foo # this is a lie - is not real location
/var/lib/vim/addons # correct location from external pwd
$ ls .. # shows content of /var/lib/vim, not /tmp
addons
$ cd .. ; pwd # incorrectly goes back to /tmp, not up to /var/lib/vim
/tmp
As you can see above, the built-in pwd
shows your current working directory incorrectly, and cd ..
doesn’t behave correctly, based on your actual current working directory. Confusingly, ls ..
and cd ..
access different directories!
With -o physical
all the above problems go away. The ls ..
and cd ..
both work as expected:
$ set -o physical # turn ON physical
$ cd /tmp
$ ln -s -f /var/lib/vim/addons foo ; ls -l foo
lrwxrwxrwx 1 xxx xxx 19 Mar 16 15:17 foo -> /var/lib/vim/addons
$ cd foo ; pwd ; /bin/pwd # use built-in pwd and external pwd
/var/lib/vim/addons # correct location from built-in pwd
/var/lib/vim/addons # correct location from external pwd
$ ls .. # shows content of /var/lib/vim, as expected
addons
$ cd .. ; pwd # correctly goes up to /var/lib/vim, as expected
/var/lib/vim
Read this for a Full Discussion of option Physical
set -o interactive-comments
IndexAllow #
to comment out any following text when typed interactively, e.g. echo a b # this text is not seen
Do not set any aliases that you do not understand!
If you don’t know the meaning of an alias, don’t use it. You can RTFM in command man pages to learn about options to commands.
Below are described several BASH shell aliases for a sysadmin that you might put in the interactive section of your .bashrc
(below the exit if not interactive
line):
alias l='less'
alias mv='mv -i'
alias cp='cp -i -p'
alias ls='ls -abp --color=auto'
alias grep='grep --color=auto'
alias fgrep='grep -F --color=auto'
RTFM to understand what each alias does.
DO NOT alias rm
to rm -i
, since it requires you to confirm every removal and quickly loses any protective value as you get used to always typing yes
without thinking.
Do not change any history settings that you do not understand!
Many of these useful settings may already be set by your instructor. Don’t change anything that is already working!
As system administrators, having a long history of previous commands available is useful. The number of commands to keep in memory and number of commands to save to disk (in the .bash_history
file) are controlled by the BASH shell variables HISTSIZE
and HISTFILESIZE
.
Having all your shells append their history, rather than overwrite the history file, is also useful. Having the shells append the history after each command, instead of waiting until the shell exits, is also helpful.
Below are some suggested settings to preserve a long history. Many of these may already be set in your account by your instructor:
export HISTSIZE=2000
export HISTFILESIZE=1000000
export HISTCONTROL=ignoredups
export HISTTIMEFORMAT=
export PROMPT_COMMAND='history -a'
shopt -s cmdhist
shopt -s histappend
See man bash
for the details on all the above variables and options.
.bash_profile
and .bashrc
IndexDo not set any options or aliases in your .bashrc
that you do not understand! If you don’t know the meaning of a setting, don’t use it. You can RTFM in the bash
man page for all BASH settings, and RTFM in command man pages to learn about options to commands.
.bash_profile
– only one line:
source ./.bashrc
.bashrc
– two lines minimum:
# REQUIRED LINE: This first line is required and must be the first line.
[ -z "${PS1-}" ] && return # exit if not interactive
# REQUIRED LINE: This PS1 prompt setting (or one like it) is required.
PS1='[\u@\h \W]\$ ' # include PWD in shell prompt
# Optional: DO NOT USE ANY OF THESE if you don't know what they DO!
shopt -s failglob
set -o ignoreeof
set -o noclobber
set -o notify
set -o nounset
set -o physical
set -o interactive-comments
# Optional: DO NOT USE ANY OF THESE if you don't know what they DO!
alias l='less'
alias mv='mv -i'
alias cp='cp -i -p'
alias ls='ls -abp --color=auto'
alias grep='grep --color=auto'
alias fgrep='grep -F --color=auto'
# Optional: You can use these safely for better shell history:
# (These settings may already be set by your instructor.)
export HISTSIZE=2000
export HISTFILESIZE=1000000
export HISTCONTROL=ignoredups
export HISTTIMEFORMAT=
export PROMPT_COMMAND='history -a'
shopt -s cmdhist
shopt -s histappend
Your .bashrc
file must not produce any output when read by a non-interactive shell. File transfer programs will fail if your .bashrc
file produces output that corrupts the file transfer.
Your profile files are not re-read by your current shell after you edit them – you have to tell your current shell to re-read the file to make the new content take effect in the current shell, e.g. type source ./.bashrc
Do not set the PS1
prompt string in any start-up file unless it is already set, indicating an interactive shell. Do not produce any output in any start-up file unless the shell is interactive (has PS1
set non-empty). The exit if not interactive
line ensures this.
You can choose any PS1
prompt string you like.