Challenge - 5 Problems
Shift Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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"
Attempts:
2 left
💡 Hint
Remember that
shift moves all arguments to the left by one position.✗ Incorrect
Initially, $1 is 'one'. After
shift, $1 becomes the original $2, which is 'two'. So the outputs are 'one' then 'two'.💻 Command Output
intermediate2: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 "$#"
Attempts:
2 left
💡 Hint
Each shift removes one argument from the front.
✗ Incorrect
Starting with 4 arguments, shifting by 2 removes the first two, leaving 2 arguments.
📝 Syntax
advanced2: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 2Attempts:
2 left
💡 Hint
Shift count must be a non-negative integer.
✗ Incorrect
Using a negative number with shift is invalid syntax and causes an error.
🚀 Application
advanced2: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
Attempts:
2 left
💡 Hint
The loop prints $1 then shifts until no arguments remain.
✗ Incorrect
The loop prints each argument in order, shifting after each print, so outputs 'a', then 'b', then 'c'.
🧠 Conceptual
expert2: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?Attempts:
2 left
💡 Hint
Shifting more than available arguments removes all arguments without error.
✗ Incorrect
Shifting more than the number of arguments sets $# to zero without error.