preface
The shell has some predefined variables that have special meaning, such as positional parameters.
Special variables
Positional arguments
${n} ${n} ${n} ${n} ${n} ${n} ${n} ${n} ${n}
#! /bin/bash
echo "name: $0"
echo "param1: The $1"
echo "param2: $2"
echo "param3: $3"
echo "param4: $4"
echo "param5: A $5"
echo "param6: $6"
echo "param7: $7"
echo "param8: $8"
echo "param9: $9"
echo "param10: The ${10}"
echo "param11: The ${11}"
Copy the code
Other special variables
$# : The number of arguments passed to the script
Run the other_position.sh script with three parameters: 111 222 333:
#! /bin/bash
echo The number of parameters can be used to calculate the number of parameters.$# : $#"
Copy the code
And $@ $* : The result looks the same. The difference is that $@ in double quotes represents each command line argument, which can be viewed as an array. The quoted $* is a string of command line arguments.
If they weren’t in double quotes, it would all be the same. I’m not sure if the instructions here are correct, but look at the effect:
#! /bin/bash
echo The number of parameters can be used to calculate the number of parameters.$# : $#"
echo '$@ without quotes'.$@
for v in $@
do
echo "v: $v"
done
echo '$* without quotes', $*for v in $*
do
echo "v: $v"
done
echo 'quoted "$@"'."$@"
for v in "$@"
do
echo "v: $v"
done
echo 'quoted "$*"'."$*"
for v in "$*"
do
echo "v: $v"
done
Copy the code
The single and double quotes used in the sample code will be explained later in this blog post
-
$? : Return value of the execution script
-
$$: indicates the process ID of the current process
-
$! : Executes the PID of the background process for the last time
The three examples are as follows:
#! /bin/bash
Run an ls command to see the return value
ls
echo "return , $?"
# Id of the current process
echo "current pid: $$"
# background run
echo "hello,world" &
echo "last bg pid: $!"
Copy the code