Question
What do the following environment variables in Linux mean?
What-Is
From here:
- $# Stores the number of command-line arguments that
- were passed to the shell program.
- $? Stores the exit value of the last command that was
- executed.
- $0 Stores the first word of the entered command (the
- name of the shell program).
- $* Stores all the arguments that were entered on the
- command line ($1 $2 ...).
- "$@" Stores all the arguments that were entered
- on the command line, individually quoted ("$1" "$2" ...).
As Brian commented, here is a simple example. If you run following command:
The we will have:
These are part of POSIX standard, and should be supported by all compliant shells. For the reference, below is POSIX standard definitions for each special parameter. Do note there's three additional variables: $-, $$ and $!.
- $-
- $$
- $!
Check sample bash script:
- testSV.sh
- #!/bin/bash
- echo -e "\t[Info] \$#=$#"
- echo -e "\t[Info] \$*='$*'"
- echo -e "\t[Info] \$@ is an array. Below will loop input argument(s):"
- for arg in $@
- do
- echo -e "\t\t$arg"
- done
- echo -e "\t[Info] \$0=$0, \$1=$1 ..."
- ls /notexist
- echo -e "\t[Info] Exit with none zero - $? / $!"
- ls /
- echo -e "\t[Info] Exit with zero - $? / $!"
- ps | grep "testSV.sh"
- echo -e "\t[Info] Bash with PID=$$"
- echo "$-"
Supplement
* Bash - Internal Variables
沒有留言:
張貼留言