Arguments are apple ($1), banana ($2), cherry ($3). Script name is $0.
Step 2: Understand output commands
echo prints $1 as apple, $2 as banana, and $# as 3 (number of arguments).
Final Answer:
First: apple
Second: banana
Count: 3 -> Option A
Quick Check:
Arguments match output [OK]
Hint: Remember $1, $2 are first and second args; $# is count [OK]
Common Mistakes:
Mixing script name ($0) with arguments
Confusing argument order
Miscounting number of arguments
4. Given this script snippet:
echo "Argument 1: $1"
echo "Argument 2: $2"
What is the error if you run the script without any arguments?
medium
A. Script prints empty lines for $1 and $2
B. No output is printed
C. Script throws a syntax error
D. Script prints the script name instead of arguments
Solution
Step 1: Understand missing arguments behavior
If no arguments are passed, $1 and $2 are empty strings, not errors.
Step 2: Check script output
echo prints "Argument 1: " and "Argument 2: " with nothing after colon.
Final Answer:
Script prints empty lines for $1 and $2 -> Option A
Quick Check:
Missing args = empty $1, $2 [OK]
Hint: No args means $1, $2 are empty, no errors [OK]
Common Mistakes:
Expecting syntax or runtime error
Thinking script name prints as argument
Assuming no output at all
5. You want a bash script to print all arguments passed to it, each on a new line, using only $1, $2, etc. Which approach works best?
hard
A. Use a loop with a counter and access arguments by $1, $2, ... until counter > $#
B. Use shift command in a loop to access each argument and print it
C. Use $0 to print all arguments one by one
D. Use echo $@ to print all arguments on one line
Solution
Step 1: Understand how to access all arguments one by one
Using shift moves arguments left, so $1 becomes next argument each time.
Step 2: Compare options
Use a loop with a counter and access arguments by $1, $2, ... until counter > $# is complex and error-prone; Use echo $@ to print all arguments on one line prints all on one line; Use $0 to print all arguments one by one prints script name; Use shift command in a loop to access each argument and print it correctly loops and prints each argument on new line.
Final Answer:
Use shift command in a loop to access each argument and print it -> Option B
Quick Check:
Shift loops through args one by one [OK]
Hint: Use shift in loop to print each argument separately [OK]
Common Mistakes:
Using $0 instead of arguments
Trying to manually count without shift
Printing all args on one line instead of separate lines
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.