Run ./deploy.sh blue west.
Retrieve the number of positional arguments, which is 2.
Which special parameter is appropriate?
$# returns the number of positional arguments passed to a script or function. $0 is not included in that count.
Detailed explanation
$0Incorrect. $0 is the script name.
Incorrect. $0 is the script name.
$?Incorrect. $? is the previous command's exit status.
Incorrect. $? is the previous command's exit status.
$#Correct. $# is 2 for blue and west.
Correct. $# is 2 for blue and west.
$$Incorrect. $$ is the shell process ID.
Incorrect. $$ is the shell process ID.
Try it yourself
An example you can run in a temporary verification environment.
sh -c 'printf "%s\n" "$#"' deploy.sh blue westExpected result
2Key points
- $# is the argument count
- $0 is separate
- Useful for input validation
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.