Pages

Friday, April 28, 2017

Use of shopt command

1) SHOPT

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.
shopt is a shell builtin command to set and unset (remove) various Bash shell options. To see current settings, type:

# shopt

Sample outputs:

autocd           off
sourcepath               on
xpg_echo                  off

To enable (set) option use the following command:
# shopt -s histappend

To disable (unset) option use the following command:

# shopt -u histappend

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 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 the previous sessions). Do you want to know what time you executed your commands in history

PROBLEM WITH SPELL CHECKS?

Example 6: 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
pwd
/var/ftp

PROBLEM WITH ECHO COMMAND?

Example 7: 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 line\n now on second line”
This is first line\n 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 another things your shopt command will do. A Good Reference for all the options:

No comments:

Post a Comment