乐闻世界logo
搜索文章和话题

What are the meanings of special variables $0, $1, $2, $@, $*, $#, $? in Shell scripts?

3月6日 21:29

The meanings of special variables $0, $1, $2, $@, $*, $#, $? in Shell scripts are as follows:

Positional Parameter Variables

$0

  • Meaning: The filename of the current script
  • Usage: Get the script's own name or path
  • Example:
bash
#!/bin/bash echo "Script name: $0" # Output: Script name: ./script.sh

$1, $2, $3, ..., ${10}, ${11}, ...

  • Meaning: Arguments passed to the script
  • $1: First argument
  • $2: Second argument
  • ${10}: Tenth argument (requires curly braces)
  • Example:
bash
#!/bin/bash echo "First argument: $1" echo "Second argument: $2" echo "Tenth argument: ${10}" # Execute: ./script.sh arg1 arg2 # Output: First argument: arg1 # Second argument: arg2

Special Parameter Variables

$#

  • Meaning: Number of arguments passed to the script
  • Usage: Check argument count or iterate through all arguments
  • Example:
bash
#!/bin/bash echo "Number of arguments: $#" # Execute: ./script.sh arg1 arg2 arg3 # Output: Number of arguments: 3

$@

  • Meaning: All positional parameters as separate strings
  • Feature: Each parameter remains independent, won't merge when quoted
  • Example:
bash
#!/bin/bash for arg in "$@"; do echo "Argument: $arg" done # Execute: ./script.sh "hello world" "foo bar" # Output: Argument: hello world # Argument: foo bar

$*

  • Meaning: All positional parameters as a single string
  • Feature: All parameters merged into one string, separated by the first character of IFS
  • Example:
bash
#!/bin/bash echo "All arguments: $*" # Execute: ./script.sh arg1 arg2 arg3 # Output: All arguments: arg1 arg2 arg3

$?

  • Meaning: Exit status code of the last command
  • Usage: Check if a command executed successfully
  • Return value: 0 indicates success, non-zero indicates failure
  • Example:
bash
#!/bin/bash ls /nonexistent echo "Exit status: $?" # Output: ls: /nonexistent: No such file or directory # Exit status: 1

$$

  • Meaning: PID (Process ID) of the current Shell process
  • Usage: Create unique temporary filenames or process management
  • Example:
bash
#!/bin/bash echo "Current PID: $$" # Output: Current PID: 12345

$!

  • Meaning: PID of the most recent background process
  • Usage: Track background processes
  • Example:
bash
#!/bin/bash sleep 10 & echo "Background PID: $!"

Other Special Variables

$-

  • Meaning: Current Shell option flags
  • Example:
bash
echo $- # Output: himBH (indicates enabled options)

$_

  • Meaning: Last argument of the previous command
  • Example:
bash
ls /etc/passwd echo $_ # Output: /etc/passwd

Practical Application Example

bash
#!/bin/bash # Check argument count if [ $# -lt 2 ]; then echo "Usage: $0 <arg1> <arg2>" exit 1 fi echo "Script: $0" echo "Total arguments: $#" echo "All arguments (\$@):" for arg in "$@"; do echo " - $arg" done echo "All arguments (\$*): $*" echo "First arg: $1" echo "Second arg: $2" # Execute command and check status ls "$1" if [ $? -eq 0 ]; then echo "Command succeeded" else echo "Command failed" fi

Summary of Differences Between $@ and $*

Feature$@$*
Quoted wrappingEach parameter independentAll parameters merged
Space handlingPreservedMay be merged
Recommended useYesNo

Best Practice: Always use "$@" when iterating through arguments to correctly handle arguments containing spaces.

标签:Shell