Linux shell programming : special variables
These are special shell variables which are set internally by the shell and which are available to the user:
Variable | Description |
---|---|
$0 | The filename of the current script. |
$n | These variables correspond to the arguments with which a script was invoked. Here n is a positive decimal number corresponding to the position of an argument (the first argument is $1, the second argument is $2, and so on). |
$$ | The process ID of the current shell. For shell scripts, this is the process ID under which they are executing. |
$# | The number of arguments supplied to a script. |
$@ | All the arguments are individually double quoted. If a script receives two arguments, $@ is equivalent to $1 $2. |
$* | All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1 $2. |
$? | The exit status of the last command executed. |
$! | The process ID of the last background command. |
$_ | The last argument of the previous command. |
Let's see how the special shell variables are working:
#!/bin/bash # special.sh echo "Process ID of shell = $$" echo "Program name = $0" echo "Number of args = $#" echo "Argument 1 = $1" echo "Argument 2 = $2" echo "Complete list of arguments = $*" echo "Complete list of arguments = $@"
Output:
$ ./special.sh arg1 arg2 arg3 Process ID of shell = 20779 Program name = ./special.sh Number of args = 3 Argument 1 = arg1 Argument 2 = arg2 Complete list of arguments = arg1 arg2 arg3 Complete list of arguments = arg1 arg2 arg3 $ echo $? 0
The $?
variable represents the exit status of the previous command.
Exit status is a numerical value returned by every command upon its completion. As a rule, most commands return an exit status of 0 if they were successful, and 1 if they were unsuccessful.
#!/bin/bash for args in $* do echo $args done
Output:
$ ./unknown_args.sh 1 2 3 4 1 2 3 4
A sample script that starts apache if it finds it off:
# launch.sh #!/bin/sh ps auxw | grep apache2 | grep -v grep > /dev/null if [ $? != 0 ] then /etc/init.d/apache2 start > /dev/null fi
Give it executable permissions:
$ chmod +x launch.sh
The process ID of the last background command:
$ ls & [1] 27931 $ echo $! 27931
The process ID of the current shell. For shell scripts, this is the process ID under which they are executing:
$ echo $$ 16487 $ ps -ef|grep 16487 k 16487 8098 0 Jun21 pts/18 00:00:01 bash $ ps -ef | grep `echo $$` k 16487 8098 0 Jun21 pts/18 00:00:01 bash
- $* and $@ when unquoted are identical and expand into the arguments.
- "$*" is a single word, comprising all the arguments to the shell, joined together with spaces. For example '1 2' 3 becomes "1 2 3".
- "$@" is identical to the arguments received by the shell, the resulting list of words completely match what was given to the shell. For example '1 2' 3 becomes "1 2" "3"
- In short, $@ when quoted ("$@") breaking up the arguments if there are spaces in them. But "$*" does not breaking the arguments.
# sp.sh #!/bin/bash len1=${#@} len2=${#*} echo "arg length len1=$len1" echo "arg length len2=$len2" echo if [ $len1 -eq 0 ] || [ $len2 -eq 0 ] then echo "No args provided..." return fi for args in $* do echo "$args " done echo for args in $@ do echo "$args " done echo for args in "$*" do echo "$args " done echo for args in "$@" do echo "$args " done
Output 1:>
$ ./sp.sh 1 2 3 4 5 arg length len1=5 arg length len2=5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Output 2:>
$ ./sp.sh 1 2 3 '4 5' arg len=4 arg len=4 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5
Here are the usage samples of {}:
- To expand a variable:
x="tri" y=${x}angle echo $y # 'triangle'
- To expand arrays:
numbers=("uno","dos","tres") echo $numbers echo ${numbers[0]} ${numbers[1]} ${numbers[2]} for item in $numbers do echo $item done for item in ${numbers[@]} do echo $item done for item in ${numbers[*]} do echo $item done
where all five echo will produceuno,dos,tres
- To expand positional parameters beyond 9:
$8 $9 ${10} ${11}
- Substring removal
${PARAMETER%PATTERN}
Sample:fullname="tiny.txt" firstname=${fullname%.txt} echo $fullname $firstname
Output:tiny.txt tiny
To see extensive usage list of "{ }" : Parameter expansion
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization