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. $# is the number of arguments passed to the function.
$1Correct. $1 is operator, the function's first positional argument.
$?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
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.