Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Shifting arguments (shift) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Shift Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output after shifting arguments?
Consider the following bash script snippet. What will be printed when the script is run with arguments: one two three?
Bash Scripting
echo "$1"
shift
echo "$1"
Aone\ntwo
Btwo\nthree
Cone\nthree
Dthree\nthree
Attempts:
2 left
💡 Hint
Remember that shift moves all arguments to the left by one position.
💻 Command Output
intermediate
2:00remaining
How many arguments remain after shifting twice?
Given a script run with 4 arguments, what is the value of $# after executing shift 2?
Bash Scripting
shift 2
echo "$#"
A3
B4
C0
D2
Attempts:
2 left
💡 Hint
Each shift removes one argument from the front.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this shift usage
Which option contains a syntax error when trying to shift arguments by 2?
Bash Scripting
shift 2
Ashift 2
Bshift 0
Cshift -2
Dshift 1
Attempts:
2 left
💡 Hint
Shift count must be a non-negative integer.
🚀 Application
advanced
2:00remaining
What is the output of this script using shift in a loop?
Given the script below, what will it print when run with arguments a b c?
Bash Scripting
while [ "$#" -gt 0 ]; do
  echo "$1"
  shift
 done
Aa\nb\nc
Ba\na\na
Cc\nb\na
Db\nc
Attempts:
2 left
💡 Hint
The loop prints $1 then shifts until no arguments remain.
🧠 Conceptual
expert
2:00remaining
What happens if you shift more than the number of arguments?
If a script is run with 2 arguments and executes shift 3, what is the value of $# afterwards?
A2
B0
C-1
D3
Attempts:
2 left
💡 Hint
Shifting more than available arguments removes all arguments without error.

Practice

(1/5)
1. What does the shift command do in a bash script?
easy
A. It moves all command-line arguments one position to the left, dropping the first argument.
B. It duplicates the first argument to the end of the list.
C. It reverses the order of all command-line arguments.
D. It prints all command-line arguments without changes.

Solution

  1. Step 1: Understand the role of shift

    The shift command removes the first argument and shifts all others left.
  2. Step 2: Effect on arguments

    After shift, the second argument becomes the first, the third becomes second, and so on.
  3. Final Answer:

    It moves all command-line arguments one position to the left, dropping the first argument. -> Option A
  4. Quick Check:

    shift drops first argument [OK]
Hint: Remember: shift drops first argument and moves others left [OK]
Common Mistakes:
  • Thinking shift duplicates arguments
  • Assuming shift reverses arguments
  • Believing shift prints arguments unchanged
2. Which of the following is the correct syntax to shift two arguments in a bash script?
easy
A. shift --2
B. shift -2
C. shift 2
D. shift 1 1

Solution

  1. Step 1: Recall shift syntax

    The correct syntax to shift by a number is shift N, where N is a positive integer.
  2. Step 2: Identify valid option

    shift 2 shifts by two arguments; negative or multiple numbers are invalid.
  3. Final Answer:

    shift 2 -> Option C
  4. Quick Check:

    Use shift N with positive N [OK]
Hint: Use 'shift N' with positive number N to shift multiple args [OK]
Common Mistakes:
  • Using negative numbers with shift
  • Trying to pass multiple numbers
  • Using invalid flags like --2
3. What will be the output of this script if run as ./script.sh apple banana cherry?
echo $1
shift
echo $1
medium
A. apple\napple
B. banana\ncherry
C. banana\napple
D. apple\nbanana

Solution

  1. Step 1: Check initial $1 value

    Before shift, $1 is the first argument: 'apple'.
  2. Step 2: Apply shift and check new $1

    After shift, $1 becomes the second argument: 'banana'.
  3. Final Answer:

    apple\nbanana -> Option D
  4. Quick Check:

    First echo prints first arg, second echo prints second arg after shift [OK]
Hint: First echo shows $1, shift moves args left, second echo shows new $1 [OK]
Common Mistakes:
  • Assuming shift does not change $1
  • Confusing argument positions after shift
  • Expecting output to be 'banana\ncherry'
4. Identify the error in this script snippet:
shift 3
echo $1
Assuming the script is run with only two arguments.
medium
A. Error: shift 3 tries to shift more arguments than exist, causing an error.
B. No error; it prints the first argument unchanged.
C. Error: shift requires a negative number to work.
D. No error; it prints the third argument.

Solution

  1. Step 1: Check number of arguments

    The script has only two arguments but tries to shift by three.
  2. Step 2: Understand shift behavior on excess shift

    Shifting more than available arguments causes an error or empties all arguments.
  3. Final Answer:

    Error: shift 3 tries to shift more arguments than exist, causing an error. -> Option A
  4. Quick Check:

    Shifting beyond argument count causes error [OK]
Hint: Don't shift more than available arguments to avoid errors [OK]
Common Mistakes:
  • Assuming shift works with negative numbers
  • Ignoring argument count before shifting
  • Expecting shift to print unchanged arguments
5. You want to process all command-line arguments one by one in a bash script using shift. Which script snippet correctly does this?
hard
A. for arg in "$@"; do shift; echo "$arg"; done
B. while [ "$#" -gt 0 ]; do echo "$1"; shift; done
C. shift; while [ "$#" -gt 0 ]; do echo "$1"; done
D. while [ "$#" -ge 1 ]; do echo "$2"; shift 2; done

Solution

  1. Step 1: Understand argument count and shifting

    Use while [ "$#" -gt 0 ] to loop while arguments remain.
  2. Step 2: Print first argument and shift each iteration

    Inside loop, print $1 then shift to move to next argument.
  3. Final Answer:

    while [ "$#" -gt 0 ]; do echo "$1"; shift; done -> Option B
  4. Quick Check:

    Loop with shift processes all args one by one [OK]
Hint: Use while loop with $# and shift to process args one by one [OK]
Common Mistakes:
  • Using for loop with shift inside causing skipped args
  • Not updating argument count in loop condition
  • Shifting by 2 causing missed arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard