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
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.
Example4: To get help on shopt command
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.
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
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:
Latest posts by Surendra Anne (see all)
- Puppet: How to install a package/software - October 16, 2016
- Luke Kanies is no more Puppet CEO - September 27, 2016
- Puppet terminology for newbe Linux users - September 3, 2016
- Understanding AWS “The services” Part – II - June 8, 2016
- Understanding AWS(Amazon Web services) “The Cloud” Part – I - May 11, 2016