Setting shell options

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Changing bash promptHomeSetting system wide shell options →
  • Task: Make changes to your bash shell environment using set and shopt commands.
  • The set and shopt command controls several values of variables controlling shell behavior.

List currently configured shell options

Type the following command:

set -o

Sample outputs:

allexport      	off
braceexpand    	on
emacs          	on
errexit        	off
errtrace       	off
functrace      	off
hashall        	on
histexpand     	on
history        	on
ignoreeof      	off
interactive-comments	on
keyword        	off
monitor        	on
noclobber      	off
noexec         	off
noglob         	off
nolog          	off
notify         	off
nounset        	off
onecmd         	off
physical       	off
pipefail       	off
posix          	off
privileged     	off
verbose        	off
vi             	off
xtrace         	off
  • See set command for detailed explanation of each variable.

How do I set and unset shell variable options?

To set shell variable option use the following syntax:

set -o variableName

To unset shell variable option use the following syntax:

set +o variableName


Disable <CTRL-d> which is used to logout of a login shell (local or remote login session over ssh).

set -o ignoreeof

Now, try pressing [CTRL-d] Sample outputs:

Use "exit" to leave the shell.

Turn it off, enter:

set +o ignoreeof

shopt command

You can turn on or off the values of variables controlling optional behavior using the shopt command. To view a list of some of the currently configured option via shopt, enter:

shopt -p

Sample outputs:

cdable_vars    	off
cdspell        	off
checkhash      	off
checkwinsize   	on
cmdhist        	on
compat31       	off
dotglob        	off
execfail       	off
expand_aliases 	on
extdebug       	off
extglob        	off
extquote       	on
failglob       	off
force_fignore  	on
gnu_errfmt     	off
histappend     	off
histreedit     	off
histverify     	off
hostcomplete   	on
huponexit      	off
interactive_comments	on
lithist        	off
login_shell    	off
mailwarn       	off
no_empty_cmd_completion	off
nocaseglob     	off
nocasematch    	off
nullglob       	off
progcomp       	on
promptvars     	on
restricted_shell	off
shift_verbose  	off
sourcepath     	on
xpg_echo       	off

How do I enable (set) and disable (unset) each option?

To enable (set) each option, enter:

shopt -s optionName

To disable (unset) each option, enter:

shopt -u optionName


If cdspell option set, minor errors in the spelling of a directory name in a cd command will be corrected. The errors checked for are transposed characters, a missing character, and one character too many. If a correction is found, the corrected file name is printed, and the command proceeds. For example, type the command (note /etc directory spelling):

cd /etcc

Sample outputs:

bash: cd: /etcc: No such file or directory

Now, turn on cdspell option and try again the same cd command, enter:

shopt -s cdspell
cd /etcc

Sample outputs:

[vivek@vivek-desktop /etc]$

Customizing Bash environment with shopt and set

Edit your ~/.bashrc, enter:

vi ~/.bashrc

Add the following commands:

# Correct dir spellings
shopt -q -s cdspell
# Make sure display get updated when terminal window get resized
shopt -q -s checkwinsize
# Turn on the extended pattern matching features 
shopt -q -s extglob
# Append rather than overwrite history on exit
shopt -s histappend
# Make multi-line commandsline in history
shopt -q -s cmdhist 
# Get immediate notification of background job termination
set -o notify 
# Disable [CTRL-D] which is used to exit the shell
set -o ignoreeof
# Disable core files
ulimit -S -c 0 > /dev/null 2>&1

How do I setup environment variables?

Simply add the settings to ~/.bashrc:

 # Store 5000 commands in history buffer
export HISTSIZE=5000
# Store 5000 commands in history FILE 
export HISTFILESIZE=5000      
# Avoid duplicates in hisotry 
export HISTIGNORE='&:[ ]*'
# Use less command as a pager
export PAGER=less
# Set vim as default text editor
export EDITOR=vim
export VISUAL=vim
# Oracle database specific 
export ORACLE_HOME=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server
export NLS_LANG=$($ORACLE_HOME/bin/
export JAVA_HOME=/usr/lib/jvm/java-6-sun/jre
# Add ORACLE, JAVA and ~/bin bin to PATH
# Secure SSH login stuff using keychain
# No need to input password again ever
/usr/bin/keychain $HOME/.ssh/id_dsa
source $HOME/.keychain/$HOSTNAME-sh
# Turn on Bash command completion
source /etc/bash_completion
# MS-DOS / XP cmd like stuff
alias edit=$VISUAL
alias copy='cp'
alias cls='clear'
alias del='rm'
alias dir='ls'
alias md='mkdir'
alias move='mv'
alias rd='rmdir'
alias ren='mv'
alias ipconfig='ifconfig'
# Other Linux stuff
alias bc='bc -l'
alias diff='diff -u'
# get updates from RHN
alias update='yum -y update'
# set eth1 as default
alias dnstop='dnstop -l 5  eth1'
alias vnstat='vnstat -i eth1'
# force colorful grep output
alias grep='grep --color'
# ls stuff
alias l.='ls -d .* --color=tty'
alias ll='ls -l --color=tty'
alias ls='ls --color=tty'
← Changing bash promptHomeSetting system wide shell options →