Bird
0
0

You want a bash function swap that prints its two arguments in reverse order. Which function definition achieves this?

hard🚀 Application Q8 of 15
Bash Scripting - Functions
You want a bash function swap that prints its two arguments in reverse order. Which function definition achieves this?
Aswap() { echo "$2 $1"; }
Bswap() { echo "$1 $2"; }
Cswap() { echo "$3 $4"; }
Dswap() { echo "$0 $1"; }
Step-by-Step Solution
Solution:
  1. Step 1: Understand the goal

    The function should print the second argument first, then the first argument.
  2. Step 2: Match the correct argument order

    Using $2 $1 prints arguments in reverse order. Other options print original order or wrong variables.
  3. Final Answer:

    swap() { echo "$2 $1"; } -> Option A
  4. Quick Check:

    Reverse order = echo $2 $1 [OK]
Quick Trick: Swap arguments by echoing $2 then $1 [OK]
Common Mistakes:
MISTAKES
  • Using $1 $2 instead of reversed order
  • Using $3 and $4 which may be empty
  • Using $0 which is script name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes