Bird
0
0

What will be the output of this script if run as ./script.sh apple banana?

medium📝 Command Output Q4 of 15
Bash Scripting - Variables
What will be the output of this script if run as ./script.sh apple banana?
echo "$0"
echo "$1"
echo "$#"
echo "$@"
Aapple banana 2 apple banana
B./script.sh apple 2 apple banana
C./script.sh banana 2 apple banana
Dscript.sh apple 1 apple banana
Step-by-Step Solution
Solution:
  1. Step 1: Understand each variable's value

    $0 is the script name with path: './script.sh', $1 is first argument 'apple', $# is number of arguments '2', $@ is all arguments 'apple banana'.
  2. Step 2: Match output lines with options

    ./script.sh apple 2 apple banana matches exactly the expected output lines.
  3. Final Answer:

    ./script.sh apple 2 apple banana -> Option B
  4. Quick Check:

    $0=script name, $1=first arg, $#=arg count, $@=all args [OK]
Quick Trick: Remember $0 is script name, $1 first arg, $# count, $@ all args [OK]
Common Mistakes:
MISTAKES
  • Confusing $0 with first argument
  • Mixing $1 and $@ outputs
  • Miscounting $# arguments

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes