SoFunction
Updated on 2025-03-10

Summary of the problem of shell special position variable

1. Special position variable $n

  • $n: represents the first passed to a script or functionnparameters.
  • $1: The first parameter
  • $2: The second parameter
  • ...
  • $9: The ninth parameter
  • If more than 9 parameters are required{},For example${10}Indicates the tenth parameter.

2. Special position variable $0

 $0: represents the file name of the script or the name of the current command or the script path

#!/bin/bash
echo "Script name is: $0"
#bash 
#The script name is:

$0 gets the script path

#!/bin/bash
# Use dirname to extract script directoryscript_dir=$(dirname "$0")
# Output script directoryecho "The script directory is: $script_dir"

The function of dirname $0:

  • Extract the directory part in the script path represented by $0.
  • When calling a script with a relative path,dirname $0The result is also relative path
  • Relative path: If you call the script through a relative path, for example./,Sodirname "$0"Will return., represents the current directory.
  • Absolute path: If the script is called through an absolute path,dirname "$0"It will return the absolute directory where the script is located.

basename $0 function:

  • Extract the file name part in the path.
  • from$0Removes the path part from the value of the value and returns only the file name.
#!/bin/bash
# Get the full path to the script#script_path=$(readlink -f "$0")
# Extract directory and namescript_dir=$(dirname "$0")
script_name=$(basename "$0")
echo "The script directory is: $script_dir"
echo "The script name is: $script_name"

3. Special position variable $ #

$#Indicates the number of parameters passed to the script, that is, the number of positional parameters.

#!/bin/bash
echo "The number of parameters passed to the script is: $#"
#bash  1 2 3
#The number of parameters passed to the script is: 3#bash  "1 2" 3
#The number of parameters passed to the script is: 2#bash  {a..z}
#The number of parameters passed to the script is:26

4. Special position variable $*/$@

  • $*: Treat all parameters as a single string.
  • $@: Treat each parameter as an independent string, especially when the parameter contains spaces, the difference is more obvious.

4.1 $*

  • $*Treat all parameters passed to the script as a single string, and are separated by spaces.
  • When there are no double quotes,$*All parameters will be processed as a single string, but it will split the parameters by spaces
#!/bin/bash
echo "When using \$*:"
for i in $*; do
  echo "[$i]"
done
bash  "Hello world" arg2 "this is" arg4
$* All parameters will be processed as a single string,But it splits parameters by space。therefore,"Hello world" Been broken up Hello and world,"this is" Been broken up this and is。
use $* hour:
[Hello]
[world]
[arg2]
[this]
[is]
[arg4]

Add double quoteshour,"$*"All parameters will be treated as a whole, but the parameters will still be separated by a space.

#!/bin/bash
echo "use \"\$*\" hour:"
for i in "$*"; do
  echo "[$i]"
done
use "$*" hour:
[Hello world arg2 this is arg4]
当加上双引号hour,"$*" Treat all parameters as a complete single string,Separate with spaces。so,The whole input becomes a large string Hello world arg2 this is arg4。

4.2 $@

$@Also represents all parameters passed to the script, but it will take each parameter as a separate string, especially when the parameter contains spaces, each parameter will be kept as a separate string.

No double quoteshour,$@Will be with$*Similarly, each parameter is treated as an independent part, and the parameters are also separated by spaces. The same behavior as $* without double quotes

#!/bin/bash
echo "When using \$@:"
for i in $@; do
  echo "[$i]"
done
#Outputuse $@ hour:
[Hello]
[world]
[arg2]
[this]
[is]
[arg4]

Add double quoteshour,"$@"It will keep each parameter independent, and will not be disassembled even if there are spaces in the parameter.

#!/bin/bash
echo "use \"\$@\" hour:"
for i in "$@"; do
  echo "[$i]"
done
use "$@" hour:
[Hello world]
[arg2]
[this is]
[arg4]
当加上双引号hour,"$@" Each parameter is retained as an independent string。Even if the parameter contains spaces(like "Hello world"),It will also turn the whole "Hello world" Handled as a parameter,Will not split。Each parameter remains independent。

Order

shiftUsed to move the position parameter to the left, the number of parameters will be reduced, the original$1Will be removed, new$2become$1, and so on.

#!/bin/bash
# Print all passed parametersecho "Original Parameters: $@"
# Move parametersshift
# Print the shifted parametersecho "Shifted parameters: $@"

AvailableshiftMultiple times to further move the parameters:

#!/bin/bash
shift 2
echo "Parameters after shifting twice: $@"
bash  arg1 arg2 arg3 arg4

This is the end of this article about shell special location variables. For more relevant shell special location variables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!