Bird
Raised Fist0
Linux CLIscripting~20 mins

Shell concept (Bash, Zsh) in Linux CLI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Shell Scripting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1:30remaining
What is the output of this Bash command?
Consider the following command run in Bash shell:

echo $((3 + 5 * 2))

What will be printed on the screen?
Linux CLI
echo $((3 + 5 * 2))
A13
B16
C10
DError: invalid arithmetic expression
Attempts:
2 left
💡 Hint
Remember the order of operations in arithmetic expressions.
📝 Syntax
intermediate
1:30remaining
Which option correctly defines a function in Bash?
You want to create a function named greet that prints 'Hello'. Which of these is the correct syntax?
Afunction greet { echo 'Hello'; }
Bdef greet() { echo 'Hello' }
Cgreet() echo 'Hello'
Dfunction greet() echo 'Hello';
Attempts:
2 left
💡 Hint
Bash functions can be declared with or without the 'function' keyword, but syntax matters.
🔧 Debug
advanced
2:00remaining
Why does this script fail to print the variable value?
Given this script:

#!/bin/bash VAR=5 if [ $VAR -gt 3 ] then echo "Value is $var" fi

What is the reason for the output being empty?
Linux CLI
#!/bin/bash
VAR=5
if [ $VAR -gt 3 ]
then
  echo "Value is $var"
fi
AThe script lacks execution permission, so it doesn't run.
BVariable names are case-sensitive; $var is empty because VAR was set, not var.
CThe if condition syntax is wrong; it should use double brackets [[ ]].
DThe echo command is missing quotes around $var.
Attempts:
2 left
💡 Hint
Check how variable names are written and used.
🚀 Application
advanced
2:00remaining
Which command lists all files including hidden ones in long format sorted by modification time?
You want to see all files, including hidden ones (those starting with a dot), in your current directory. You want the list in long format and sorted by newest modified first. Which command achieves this?
Als -l -a -t
Bls -ltr
Cls -lat
Dls -l -a -r -t
Attempts:
2 left
💡 Hint
Check what each option means: -l, -a, -t, -r
🧠 Conceptual
expert
2:00remaining
What happens when you run this pipeline in Bash?
Consider this command:

echo 'apple banana cherry' | while read word; do echo $word; done

What will be the output?
A
apple
banana
cherry
B
apple banana cherry
apple banana cherry
CNo output, the loop does not run
Dapple
Attempts:
2 left
💡 Hint
Think about how 'read' works with default IFS and how the pipeline feeds input.

Practice

(1/5)
1. What is a shell in Linux or macOS?
easy
A. A programming language for creating websites
B. A program that lets you type commands to control your computer
C. A graphical interface for editing photos
D. A type of hardware component inside the computer

Solution

  1. Step 1: Understand the role of a shell

    A shell is a program that takes your typed commands and runs them on the computer.
  2. Step 2: Compare options

    Options A, B, and C describe web programming, hardware, and graphics, which are not shells.
  3. Final Answer:

    A program that lets you type commands to control your computer -> Option B
  4. Quick Check:

    Shell = command program [OK]
Hint: Shell = command line program to control computer [OK]
Common Mistakes:
  • Thinking shell is hardware
  • Confusing shell with graphical apps
  • Mixing shell with programming languages
2. Which of these is the correct way to run a command with an option in Bash?
easy
A. ls -ll
B. ls -l -
C. ls l-
D. ls -l

Solution

  1. Step 1: Identify correct option syntax

    Options start with a dash (-) followed by letters, so '-l' is correct.
  2. Step 2: Check each option

    A: 'ls -l' is correct. B has extra dash, C has wrong order, D uses '-ll' which repeats the option unnecessarily.
  3. Final Answer:

    ls -l -> Option D
  4. Quick Check:

    Options start with dash and letter [OK]
Hint: Options start with dash (-) followed by letters [OK]
Common Mistakes:
  • Adding extra dash after options
  • Writing options without dash
  • Combining options incorrectly
3. What will the following Bash command output?
echo "Hello"; echo "World"
medium
A. Hello World (two lines)
B. Hello\nWorld
C. Hello World
D. Hello World

Solution

  1. Step 1: Understand the echo command

    Each echo prints its argument followed by a new line by default.
  2. Step 2: Analyze the command sequence

    First echo prints 'Hello' then newline, second echo prints 'World' then newline, so output is two lines.
  3. Final Answer:

    Hello World (two lines) -> Option A
  4. Quick Check:

    echo prints with newline [OK]
Hint: echo prints text then newline by default [OK]
Common Mistakes:
  • Thinking echo joins output on one line
  • Confusing literal \n with newline
  • Ignoring semicolon separates commands
4. What is wrong with this Bash script snippet?
for i in 1 2 3
do
echo $i
done
medium
A. Missing semicolon after '3'
B. Incorrect loop syntax, should use parentheses
C. No error, script runs fine
D. Missing 'do' keyword

Solution

  1. Step 1: Check for loop syntax

    The for loop syntax 'for i in 1 2 3; do ... done' is correct. The semicolon after '3' is optional if 'do' is on next line.
  2. Step 2: Verify keywords and structure

    'do' is present, parentheses are not needed in Bash for this loop, so no syntax error.
  3. Final Answer:

    No error, script runs fine -> Option C
  4. Quick Check:

    Bash for loop syntax correct without semicolon if newline before do [OK]
Hint: Newline before 'do' replaces semicolon in for loops [OK]
Common Mistakes:
  • Thinking semicolon is always required
  • Confusing Bash syntax with other languages
  • Missing 'do' keyword
5. You want to list all files in a directory including hidden ones, sorted by modification time. Which command should you use?
hard
A. ls -a -t
B. ls -l -h
C. ls -r -S
D. ls -d .*

Solution

  1. Step 1: Understand options needed

    '-a' shows all files including hidden, '-t' sorts by modification time.
  2. Step 2: Evaluate each option

    A: 'ls -r -S' reverses and sorts by size (no hidden). B: 'ls -l -h' long format with human sizes (no hidden, no time sort). C: 'ls -a -t' shows all including hidden, sorted by mod time. D: 'ls -d .*' shows only dot entries.
  3. Final Answer:

    ls -a -t -> Option A
  4. Quick Check:

    Show hidden + sort by time = ls -a -t [OK]
Hint: Use -a for hidden, -t for time sort [OK]
Common Mistakes:
  • Using -l instead of -a to show hidden
  • Confusing -S (size) with -t (time)
  • Using -d .* lists only hidden files
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