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. $# is the number of positional arguments.
$@Incorrect. $@ expands all positional arguments.
$0Correct. $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
Shell function
A reusable group of commands that runs in the caller's shell context.
Positional parameter
A numbered argument or special value supplied to a script or function.
Control flow
Conditions, loops, and exit statuses determine which shell commands run.