The value and usage of Dollar $ in UNIX/LINUX and shell scripting
Scenario1:
Dollar($) can be used to get the value of user-defined
variable and also system-defined variables like HOME,LOGNAME,PATH,PS1,IFS,SHELL
etc..
[root@localhost
~]# x=18
[root@localhost
~]# echo $x
18
[root@localhost
~]# echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost
~]#
Scenario2:$PS1
Some times when you login to a particular user(other than
root -> since root always has prompt #)of UNIX/LINUX server the prompt
appears as Dollar($) it’s because of the primary Prompt String(PS1) has been
set a like below .If you want you can change it as your customized
prompt.
[root@localhost
~]# echo $PS1
[\u@\h \W]\$
[root@localhost
~]# su - bharath
[bharath@localhost
~]$
Scenario3->$0:
$0
gives the name of the shell or shell script. "$0" gives the running
script name with absolute path. If you want only the script name you have
to use basename
$0 which gives the name of the script alone.
[root@localhost
ios]# echo $0
-bash
[root@localhost
ios]# echo $SHELL
/bin/bash
Scenario4->$1,$2,$3….$9:
1. These
are so-called positional parameters represents a shell-scripts command-line
arguments. Individual arguments are named by integer numbers.
2. We
also use $1,$2…. As a fields in order to extract and denote particular
fields(columns) using awk utility
so that we can do further action on extracted fields as below.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[root@localhost
ios]# ls -lrt
total 12
-rwxrwxrwx. 1
root root 45 Dec 2 12:06 sample_script
-rwxrwxrwx. 1
root root 154 Dec 3 09:46 positional1
-rwxrwxrwx. 1
root root 152 Dec 3 09:47 positional
[root@localhost
ios]# ls -lrt | awk '{print $1,$9}'
total
-rwxrwxrwx.
sample_script
-rwxrwxrwx.
positional1
-rwxrwxrwx.
positional
[root@localhost
ios]# ls -lrt | awk '{print $1,$9}' | awk '{print $2}' | xargs gzip
[root@localhost
ios]# ls -lrt
total 12
-rwxrwxrwx. 1
root root 79 Dec 2 12:06 sample_script.gz
-rwxrwxrwx. 1
root root 115 Dec 3 09:46 positional1.gz
-rwxrwxrwx. 1
root root 115 Dec 3 09:47 positional.gz
Scenario5->$#:
It gives the number of positional parameters in decimal i.e.
Total number of the passed to the shell script.
[root@localhost
ios]# cat
sample_script
Script execution:
#!
/bin/bash
[root@localhost ios]# sample_script a b c d e
echo "the
value of \$# is " $# the value of $# is 5
[root@localhost
ios]# [root@localhost ios]#
Scenario6->$@ and $*:
Both $@ and $* represents all the command line
arguments(positional parameters) at once if and only if they used in
with-out double quotes i.e. both $* and $@ gives the positional parameters,
starting from one if they used in without double quotes.
$@ and $* with-out double
quotes:
$@ with-in Double quotes represents each parameter expands
to a separate word. i.e. "$@" is equivalent to "$1"
"$2"….. , $*
with-in double quotes represents
each parameter expands to a single string i.e. "$*" is equivalent to
"$1$2….."
Scenario7->$$ :
$$ gives the process ID of the current shell.
[root@localhost
ios]# echo $$ --à bash shell
2680
[root@localhost
ios]# ps -eaf | grep "\<2680\>" | egrep -v
"(ps|grep)"
root
2680 2676 0 09:36 pts/1 00:00:00 -bash
[root@localhost
ios]#
[root@localhost
ios]# sh -à changing the shell from bash to bourne i.e. bash to sh
sh-4.1#
sh-4.1# echo $$
3135
sh-4.1# ps -eaf
| grep "\<3135\>" | egrep -v "(ps|grep)"
root
3135 2680 0 10:33 pts/1 00:00:00 sh
sh-4.1#
Scenario8->$? :
$? Gives the exit status of the most recently executed
command or shell script. If the value of $?=0 means the command executed
successfully if it is other than 0 means it indicates there is an error. $?
Variable often used in shell scripts.
[root@localhost
ios]# tar -cvf ./backup.tar *.gz
positional1.gz
positional.gz
sample_script.gz
[root@localhost
ios]# echo $?
0
[root@localhost
ios]# ls -lrt
total 24
-rwxrwxrwx. 1
root root 79 Dec 2 12:06 sample_script.gz
-rwxrwxrwx. 1
root root 115 Dec 3 09:46 positional1.gz
-rwxrwxrwx. 1
root root 115 Dec 3 09:47 positional.gz
-rw-r--r--. 1
root root 10240 Dec 3 10:42 backup.tar
Scenario9->$! :
$! Gives the process ID of the most recently executed
command in background.
[root@localhost
ios]# (sleep 50 ; who ; cal)&
[1] 3202
[root@localhost
ios]# jobs
[1]+
Running
( sleep 50; who; cal ) &
[root@localhost
ios]# echo $!
3202
Scenario
10->$-(hyphen):
$- is a special variable used to represent the currently
enabled shell options as below. Each options short option letter appears in the
string if that option is enabled.
[root@localhost
~]# echo $-
himBH
-h => Remember
the location of commands as they are looked up.(hash command enabled).
-i => shell
is interactive shell.
-m => Job
control is enabled.
-B => the
shell will perform Brace expansion
-H => Enable
! style history substitution. This flag is on
Scenario 11->
$_(underscore):
$_ gives the Last argument of the previous foreground
executed command. If no argument given $_ displays the command itself.
Scenario 12->$< :
$< is used for reading standard input. $< is a
special c-shell variable used to read standard input into a variable.
Scenario 13-> $ in
Regex :
$ in Regular expression is one of the Anchor
character(another one is ^) used to match the end of line. Whereas ^ is used to
match beginning of a line.
Scenario 14->$ in
Arithmetic expansion:
$ used to do the arithmetic calculations in the command-line
as well as in scripting.
[root@localhost
~]# echo $((x=12*60+30))
750
[root@localhost
~]#
[root@localhost
~]# echo $(( 3<4 || 5>6))
1
[root@localhost
~]# echo $(( 3<4 && 5<6))
1
[root@localhost
~]# echo $(( 3<4 && 5>6))
0
[root@localhost
~]#
Comments
Post a Comment