0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.