Use of set and unset command in linux bash shell.

In this article, we will see the usage of set and unset command in Linux specific to bash shell.

I. Set Command
Calling set without any argument displays names and values of all shell variables which are sorted by name.
Lets have a look at the command.

[root@nglinux ~]# set
ARCH=i686
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:login_shell:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="2" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='4.1.2(2)-release'
COLORS=/etc/DIR_COLORS.256color
COLUMNS=80
DIRSTACK=()
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
HISTCONTROL=ignoredups
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=nglinux
HOSTTYPE=i386
ID=0
IFS=' '
LANG=en_US.UTF-8
LC_CTYPE=UTF-8
LESSOPEN='||/usr/bin/lesspipe.sh %s'
LIBGL_DRIVERS_PATH=/usr/lib/dri
LINES=31
LOGNAME=root
LS_COLORS='rs=0:di=38;5;27:ln=38;5;51:mh=44;38;5;15:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=05;48;5;232;38;5;15:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;34:*.tar=38;5;9:*.tgz=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.Z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lz=38;5;9:*.xz=38;5;9:*.bz2=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.bz=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.rar=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.axv=38;5;13:*.anx=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.axa=38;5;45:*.oga=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:'
MACHTYPE=i386-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=3336
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/root
SELINUX_LEVEL_REQUESTED=
SELINUX_ROLE_REQUESTED=
SELINUX_USE_CURRENT_RANGE=
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SSH_CLIENT='172.21.49.169 49953 22'
SSH_CONNECTION='172.21.49.169 49953 172.21.49.220 22'
SSH_TTY=/dev/pts/0
TERM=xterm-256color
UID=0
USER=root
_=+x
colors=/root/.dircolors
str='welcome:to:new:generation enterprise linux'
word=linux
__udisks () 
{ 
    local IFS='
';
    local cur="${COMP_WORDS[COMP_CWORD]}";
    if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--show-info" ]; then
        COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
    else
        if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--inhibit-polling" ]; then
            COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
        else
            if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--mount" ]; then
                COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
            else
                if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--unmount" ]; then
                    COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
                else
                    if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--detach" ]; then
                        COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
                    else
                        if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-refresh" ]; then
                            COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
                        else
                            if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-simulate" ]; then
                                _filedir || return 0;
                            else
                                if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--set-spindown" ]; then
                                    COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
                                else
                                    if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--poll-for-media" ]; then
                                        COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
                                    else
                                        COMPREPLY=($(IFS=: compgen -W "--dump:--inhibit-polling:--inhibit-all-polling:--enumerate:--enumerate-device-files:--monitor:--monitor-detail:--show-info:--help:--mount:--mount-fstype:--mount-options:--unmount:--unmount-options:--detach:--detach-options:--ata-smart-refresh:--ata-smart-wakeup:--ata-smart-simulate:--set-spindown:--set-spindown-all:--spindown-timeout:--poll-for-media" -- $cur));
                                    fi;
                                fi;
                            fi;
                        fi;
                    fi;
                fi;
            fi;
        fi;
    fi
}
crontab () 
{ 
    if [ $1 == "-r" -o $1 == "-sr" -o $1 == "-rs" ]; then
        echo "-r option removes the crontab file.";
        echo "To remove the file use the option '-ir' to interactively remove crontab file";
    else
        command crontab $1;
    fi
}
[root@nglinux ~]# 

 

II. Set Command Syntax
Set command has many options, lets have a look at these options.

set [--aefhkntuvx[argument]]...

In bourne shell, the set built-in command options are as follows:
-- 	An option of a double-dash ("--") signifies the end of an option list. This option is primarily useful when values listed after the options will start with a dash themselves.
-a 	Mark variables that are modified or created for "export"; environment variables set in this way will be passed on to the environments of any subsequent commands.
-e 	Exit immediately if a command exits with a non-zero exit status.
-f 	Disable file name generation (globbing).
-h 	Locate and remember function commands as functions are defined (function commands are normally located when the function is executed).
-k 	All keyword arguments are placed in the environment for a command.
-n 	Read commands, however do not execute them.
-t 	Exit after executing one command.
-u 	Considers unset variables as an error when substituting.
-v 	Verbose Mode. Prints input lines as they are read.
-x 	Print mode. Prints commands and their arguments executed.

Using + rather than - causes these flags to be turned off. 
All of the set options except ±A, -s, –, and –– are shell flags. Shell flags can also be set on the sh command line at invocation.

 

III. Set Command Example.

### Turn on command debugging.
### Enable debugging mode
[root@nglinux ~]# set -x
++ printf '\033]0;%s@%s:%s\007' root nglinux '~'

### Even when we press return below line automatically printed
[root@nglinux ~]# 
++ printf '\033]0;%s@%s:%s\007' root nglinux '~'

### See the debugging of "echo hello" command.
[root@nglinux ~]# echo hello
+ echo hello
hello
++ printf '\033]0;%s@%s:%s\007' root nglinux '~'

### Similarly how ls command prints the text.
[root@nglinux ~]# ls
+ ls --color=auto
Desktop    Public           c_programs  deleteperm-1.0          nohup.out
Documents  Templates        core.2579   deleteperm_v1.0.tar.gz  rules
Downloads  Videos           core.2841   ifcfg-eth0              testdir
Music      abc1.c           core.2940   install.log.syslog
Pictures   anaconda-ks.cfg  coretest    kernel_panic.c
++ printf '\033]0;%s@%s:%s\007' root nglinux '~'

### Disable debugging mode.
[root@nglinux ~]# set +x
+ set +x
[root@nglinux ~]# 

 

IV. Unset Command
Unset command is used to unset a variable in bourne shell, unlike set command.
It doesn’t provide vice versa options as in set command.

This is separate bash builtin used to unset a variable & function.

It provides two options “-v”(in verbose mode) and “-f”(to unset function).

### Defines A variable
[root@nglinux ~]# A=hello
[root@nglinux ~]# echo $A
hello

### Unsets variable A
[root@nglinux ~]# unset A
[root@nglinux ~]# echo $A

[root@nglinux ~]# 

 

References:

https://www.computerhope.com/unix/uset.htm
https://ss64.com/bash/unset.html
https://bash.cyberciti.biz/guide/Set_command

Leave a Reply

avatar
  Subscribe  
Notify of