Change shell properties with Linux shopt command

Bash shopt command explained  with examples

Shopt(SHell OPTions) is a built-in command to change the properties of a shell such as..

Its history behavior

Spell check

Enable special characters for echo command by default

and many more.

This is an excellent command which give more control on Shell for you when you are working on bash and sh shells. This command is available after bash v2 and not available in other shells such as ksh, csh etc. Lets get familiarize with shopt command with some examples.

Example1: To enable an option with shopt you have to use -s (Set) option

shopt -s optiontype


shopt -s nullglob

Example2: To disable already set option use -u along with shopt

shopt -u optiontype

Example3: To see what are the options set or unset execute below command.



shopt -p

sample output

autocd off
cdable_vars off
cdspell off
checkhash off
checkjobs off
checkwinsize on
cmdhist on
compat31 off
compat32 off
compat40 off
compat41 off
dirspell off
dotglob off
execfail off
expand_aliases on
extdebug off
extglob off
extquote on
failglob off
force_fignore on
globstar off
gnu_errfmt off
histappend on
histreedit off
histverify off
hostcomplete on
huponexit off
interactive_comments on
lastpipe off
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

Example4: To get help on shopt command

help shopt

With this basic stuff in mind we can explore some of the good options what our shopt command will provide.

Save All your History(append but not overwrite)

Example5: Append history but not over write the history. Many of us see that some of our commands vanish or deleted in our history when we logout and login. We will be shocked to see that they are missing command in history. This is a default behavior of your shell to over write the previous sessions with the last closed session. For example you initiated two sessions, say it as session1 and session2. When you close session2 first and then session1 next, you will lost session2 history in history command, in other words we can say your session2 is over written by session1 history. If you don’t want lose your all sessions history use histappend option with shopt command as shown below. Keep this command in ~/.bashrc file.

shopt -s histappend

From now on words you can see all your history without any issue(with merged with your previous sessions). Do you want to know what time you executed your commands in history? Click here

Problem with spell checks?

Example6: shopt is an excellent command used to check spellings too. With this command we can make cd command to correct any spelling mistakes in the directory structure when changing directories. Enable cdspell option with shopt command as shown below

shopt -s cdspell

After this if you try to change directory /var/ftp but typed wrong as /var/fdp this error is taken care once the above option is set.

cd /var/fdp

Problem with echo command?

Example7:By default echo command will not understand special characters such as /n( for new line), /r( for return) etc. see below example.

echo “This is first linen now on second line”
This is first linen now on second line

If you observe n is displayed literally. To avoid this we can enable xpg_echo option as show below.

shopt -s xpg_echo

Now if you run above echo command see the output.

This is first line

now on second line

Note: This can be achieved with -e option along echo command

Glob options

Globing is a concept to expand a pattern. By default bash can understand *(For generating multiple alphanumerics ), ?(For generating single alpha-numeric) and [-](For range of chars) globing options. In shopt we have Golb(al) options which are very much useful for wildcards and other stuff. some of the as follows.

dotglob –IF set echo * command will show even hidden files too.
extglob –If set This will exclude the matches of search pattern
failglob –If set, and no matches are found, an error message is printed and the command is not executed.
nocaseglob –If set this will ignore case when listing of files.
nullglob –If set, pattern match to no files to expand to a null string, rather than themselves

There are many other things your shopt command will do. A Good Reference for all the options:


Show your love by sharing this..!
Share on VKShare on StumbleUponShare on YummlyPrint this pageFlattr the authorShare on TumblrEmail this to someoneBuffer this pageShare on Google+Share on LinkedInShare on FacebookTweet about this on TwitterPin on PinterestShare on RedditDigg this
The following two tabs change content below.
My name is Surendra Kumar Anne. I hail from Vijayawada which is capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. I work with ICE systems, Sydney as Sr. Consultant. You can contact me at surendra (@) linuxnix dot com.
  • shrinivas

    superb …ultimate post… thank you..

    • Surendra

      thanks Shrinivas

  • fasd

    I’m not sure if command line is place where you want to be lazy about what you type…

  • Sudo Nym

    Nice article, immediately bookmarked for future reference. Thanks!

    Also, did you mean “rolls on” (I myself am not an English native speaker, so “roles on” might exist…

  • Pingback: Customize your shell options « 0ddn1x: tricks with *nix()

  • Patola

    zsh beats bash more than four times:

    shopt | wc -l → 42
    print -l $options | wc -l → 180

    zsh has all that bash has and still it’s easier to use and can expand all stuff like variable names and !!-like substitutions straight on the command-line. I cannot understand why bash is so flaunted being such a inferior shell language.

  • Pingback: Links 11/8/2012: GNOME OS, OSI for Internet Freedom | Techrights()

  • Pingback: Beach Vacation Photography « The Blog of Wildstar, aka Jimpossible()

  • Raghavendra

    hi Sir,This is raghavendra. this information is very very usefull.thanku for helping all of us..

  • Prakash


    This is very helpful for linux enthusiastic.