Skip to content

Intermediate Questions

How to use these interview questions

๐Ÿง  Read each question carefully.

Try answering it yourself before expanding the answer to compare with the ideal response.

Level: Intermediate

๐ŸŸก Practical Applications & Troubleshooting.

Focus on real-world scenarios, debugging, optimization, and deeper configuration.

1. What is the syntax for a for loop in Bash?

Method 1 (Range):

for i in {1..5}; do
    echo "Number: $i"
done
Method 2 (C-style):
for ((i=1; i<=5; i++)); do
    echo "Number: $i"
done

2. How do you iterate over files in a directory?

Using a glob pattern:

for file in *.txt; do
    echo "Processing $file"
done

3. How do you declare an array in Bash?

my_array=("value1" "value2" "value3").

4. How do you access an element of an array?

${array_name[index]}.

Example: echo ${my_array[0]} prints the first element.

5. How do you get the length of an array?

${#array_name[@]}.

6. What is the difference between while and until loops?
  • while: Executes the block as long as the condition is true.
  • until: Executes the block as long as the condition is false (until it becomes true).
7. How do you create an infinite loop?

while true; do ... done or for ((;;)); do ... done.

8. How do you break out of a loop?

break.

It immediately terminates the loop execution.

9. How do you skip the current iteration and move to the next?

continue.

10. How do you read a file line-by-line?
while read -r line; do
    echo "$line"
done < filename.txt
11. What is the difference between [ condition ] and [[ condition ]]?
  • [ ] is the old, POSIX-compliant test command.
  • [[ ]] is an extended Bash keyword that supports regex, logical operators (&&, ||), and is generally safer.
12. How do you check if two strings are equal?

if [ "$str1" == "$str2" ]; then ....

Always quote variables to handle spaces/empty values correctly.

13. How do you compare integers?

Use flags: * -eq (Equal) * -ne (Not Equal) * -gt (Greater Than) * -lt (Less Than) * -ge (Greater or Equal) * -le (Less or Equal) Or use (( a > b )) for arithmetic context.

14. How do you check if a file is readable, writable, or executable?
  • -r: Readable
  • -w: Writable
  • -x: Executable Example: if [ -x script.sh ]; then ...
15. How do you extract specific columns from output?

Using awk or cut.

Example: ls -l | awk '{print $9}' prints the 9th column (filename).

16. How do you perform arithmetic operations?

$(( expression )).

Example: sum=$(( 5 + 3 )).

17. What is command substitution?

Assigning the output of a command to a variable.

Syntax: $(command). Example: date=$(date +%F).

18. How do you get the current date in specific format?

date +"%Y-%m-%d".

19. How do you check the disk usage of a specific directory?

du -sh directory_name.

-s for summary, -h for human-readable.

20. How do you find files modified in the last n minutes?

find . -type f -mmin -n.

๐Ÿงช Ready to test yourself?

๐Ÿ‘‰ Take the Intermediate Shell Scripting Quiz (Coming Soon)

๐Ÿ“ฌ DevopsPilot Weekly โ€” Learn DevOps, Cloud & Gen AI the simple way.
๐Ÿ‘‰ Subscribe here