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.
Correct. target=$1 assigns staging.
target=$0Incorrect. $0 is the script's invoked name.
Incorrect. $0 is the script's invoked name.
target=$#Incorrect. $# is the number of arguments, 2.
Incorrect. $# is the number of arguments, 2.
target=$2Incorrect. $2 is west, the second argument.
Incorrect. $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
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.