Run ./deploy.sh staging west.
Assign only staging to target.
Which assignment is appropriate?
$1 is the first positional argument and $2 is the second. Quote expansions when using values where word splitting would be unsafe.
Detailed explanation
target=$1Correct. target=$1 assigns staging.
target=$0Incorrect. $0 is the script's invoked name.
target=$#Incorrect. $# is the number of arguments, 2.
target=$2Incorrect. $2 is west, the second argument.
Try it yourself
An example you can run in a temporary verification environment.
sh -c 'target=$1; printf "%s\n" "$target"' deploy.sh staging westExpected result
stagingKey points
- $1 is the first argument
- $2 is the second
- $0 is the script name
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.