Include the script name used to invoke a usage-error message.
Which special parameter is appropriate?
$0 contains the invoked shell or script name. Combine it with basename when only the final name component is desired.
Detailed explanation
$1Incorrect. $1 is the first positional argument.
Incorrect. $1 is the first positional argument.
$#Incorrect. $# is the number of positional arguments.
Incorrect. $# is the number of positional arguments.
$@Incorrect. $@ expands all positional arguments.
Incorrect. $@ expands all positional arguments.
$0Correct. $0 identifies the invoked script.
Correct. $0 identifies the invoked script.
Try it yourself
An example you can run in a temporary verification environment.
sh -c 'printf "usage: %s ARG\n" "$0"' ./deploy.shExpected result
usage: ./deploy.sh ARGKey points
- $0 is the invoked name
- It is not a positional argument
- Useful in usage messages
Notes
- Environment: POSIX sh / 短命プロセス
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.