Some shell special parameters discussed in this article are: ∗, @, #,,!

Example 1: Use *** and ****∗ and @ to extend the positional parameter **

Used in this example script

* and ∗ and @ parameters:

[root@localhost scripts]# vim expan.sh #! /bin/bash export IFS='-' cnt=1 # Printing the data available in $* echo "Values of \"\$*\":" for arg in "$*" do echo "Arg #$cnt= $arg" let "cnt+=1" done cnt=1 # Printing the data available in $@ echo "Values of \"\$@\":" for arg in "$@" do echo "Arg #$cnt= $arg" let "cnt+=1" doneCopy the code

Here is the result:

[root@localhost scripts]# ./expan.sh "Hello world" 2 3 4
Values of "$*":
Arg #1= Hello world-2-3-4
Values of "$@":
Arg #1= Hello world
Arg #2= 2
Arg #3= 3
Arg #4= 4
Copy the code

  • Export IFS=’-‘ indicates that “-” is used to indicate internal field delimiters.

  • When printing each value of the parameter $*, it gives only one value, which is the entire positional argument separated by IFS.

  • $@ provides each parameter as a separate value.

Example 2: Count the number of positional arguments using $#

$# is the special argument that can improve the number of positional arguments in the script:

[root@localhost scripts]# vim count.sh #! /bin/bash if [ $# -lt 2 ] then echo "Usage: $0 arg1 arg2" exit fi echo -e "\$1=$1" echo -e "\$2=$2" let add=$1+$2 let sub=$1-$2 let mul=$1*$2 let div=$1/$2 echo -e "Addition=$add\nSubtraction=$sub\nMultiplication=$mul\nDivision=$div\n"Copy the code

Here is the result:

[root@localhost scripts]# ./count.sh 
Usage: ./count.sh arg1 arg2
[root@localhost scripts]# ./count.sh 2314 15241
$1=2314
$2=15241
Addition=17555
Subtraction=-12927
Multiplication=35267674
Division=0
Copy the code

If [$# -lt 2] indicates that if the number of positional parameters is less than 2, “Usage:./count.sh arg1 arg2” will be displayed.

Example 3: process-related parameters **** and $!

The $$argument gives the process ID of the shell script. $! Provides the ID of the most recently executed background process. The following example is the process ID that prints the current script and the ID of the last executed background process:

[root@localhost scripts]# vim proc.sh #! /bin/bash echo -e "Process ID=$$" sleep 1000 & echo -e "Background Process ID=$!"Copy the code

Here is the result:

[root@localhost scripts]# ./proc.sh 
Process ID=14625
Background Process ID=14626
[root@localhost scripts]# ps
   PID TTY          TIME CMD
  3665 pts/0    00:00:00 bash
 14626 pts/0    00:00:00 sleep
 14627 pts/0    00:00:00 ps
Copy the code