Call the Bash function greet as greet operator.
Print only operator from inside the function.
Which special parameter should the function use?
Inside a function, $1, $2, and so on refer to arguments passed to that function. The caller's positional parameters are restored after it returns.
Detailed explanation
$0Incorrect. $0 normally identifies the shell or script, not the first function argument.
Incorrect. $0 normally identifies the shell or script, not the first function argument.
$#Incorrect. $# is the number of arguments passed to the function.
Incorrect. $# is the number of arguments passed to the function.
$1Correct. $1 is operator, the function's first positional argument.
Correct. $1 is operator, the function's first positional argument.
$?Incorrect. $? is the previous command's exit status.
Incorrect. $? is the previous command's exit status.
Try it yourself
An example you can run in a temporary verification environment.
bash --noprofile --norc -c 'greet() { printf "%s\n" "$1"; }; greet operator'Expected result
operatorKey points
- $1 is the first argument
- $# is the argument count
- $0 is the script or shell name
Notes
- Environment: GNU Bash 5.2 / 一時シェル
- 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.