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. $? is the previous command's exit status.
$#Correct. $# is 2 for blue and west.
$$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
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.