Bash Quiz: Test Your Knowledge

Table of Contents

Introduction

This comprehensive quiz covers all aspects of Bash scripting we've discussed. Each section tests different concepts with multiple difficulty levels. Answers and explanations are provided at the end.


Section 1: Basic Commands and Navigation

Question 1.1

What does the pwd command do?
a) Print working directory
b) Password directory
c) Process working daemon
d) Previous working directory

Question 1.2

Which command is used to list files in a directory?
a) list
b) ls
c) dir
d) show

Question 1.3

What does cd ~ do?
a) Change to root directory
b) Change to home directory
c) Change to previous directory
d) Change to parent directory

Question 1.4

How do you create a new directory named "projects"?
a) create projects
b) newdir projects
c) mkdir projects
d) makedir projects

Question 1.5

What command would you use to remove a file named "temp.txt"?
a) delete temp.txt
b) rm temp.txt
c) remove temp.txt
d) del temp.txt


Section 2: File Operations

Question 2.1

What does cp -r do?
a) Copy with confirmation
b) Copy recursively (directories)
c) Copy with read permissions
d) Copy and rename

Question 2.2

Which command moves or renames files?
a) move
b) mv
c) rn
d) rename

Question 2.3

What's the difference between cp file1 file2 and mv file1 file2?
a) cp copies, mv moves
b) cp moves, mv copies
c) No difference
d) cp is faster

Question 2.4

How do you copy multiple files to a directory?
a) cp file1 file2 file3 /dest
b) copy file1,file2,file3 /dest
c) cp file1+file2+file3 /dest
d) cp [file1-file3] /dest

Question 2.5

What does rm -rf do?
a) Remove files with confirmation
b) Remove files and directories recursively with force
c) Remove read-only files
d) Remove recent files


Section 3: Text Processing

Question 3.1

What does head -n 5 file.txt do?
a) Shows the first 5 lines
b) Shows the last 5 lines
c) Shows 5 characters
d) Shows line 5

Question 3.2

Which command is used to view the end of a file?
a) end
b) tail
c) last
d) bottom

Question 3.3

What does grep "error" log.txt do?
a) Counts errors in log.txt
b) Searches for "error" in log.txt
c) Deletes errors from log.txt
d) Highlights errors in log.txt

Question 3.4

In sed 's/old/new/g' file.txt, what does the 'g' flag do?
a) Global replace (all occurrences)
b) Generate output
c) Get confirmation
d) Group matches

Question 3.5

What does wc -l file.txt output?
a) Word count
b) Line count
c) Character count
d) File size


Section 4: Permissions and Ownership

Question 4.1

What does chmod 755 script.sh do?
a) Changes owner to 755
b) Sets permissions to rwxr-xr-x
c) Deletes the script
d) Makes script executable only

Question 4.2

Which command changes file ownership?
a) chown
b) chuser
c) own
d) setowner

Question 4.3

What does chmod +x script.sh do?
a) Adds execute permission
b) Adds read permission
c) Adds write permission
d) Adds all permissions

Question 4.4

What does ls -l show?
a) Short listing
b) Long listing with permissions
c) Hidden files
d) Sorted files

Question 4.5

What permission is represented by r--r--r--?
a) Owner can read, others can read
b) Owner can read/write, others can read
c) All can read only
d) Owner can read, group can read, others can read


Section 5: Variables and Data Types

Question 5.1

How do you assign a value to a variable in Bash?
a) var = 10
b) var=10
c) set var 10
d) let var=10

Question 5.2

How do you access the value of variable name?
a) name
b) $name
c) {name}
d) (name)

Question 5.3

What does declare -i count=5 do?
a) Makes count an integer
b) Makes count an array
c) Makes count readonly
d) Makes count exported

Question 5.4

What's the difference between $@ and $*?
a) $@ is all arguments as array, $* as string
b) $@ is argument count, $* is all arguments
c) No difference
d) $@ is first argument, $* is last

Question 5.5

How do you make a variable readonly?
a) readonly var
b) const var
c) fixed var
d) immutable var


Section 6: Arrays

Question 6.1

How do you create an indexed array?
a) arr = (1 2 3)
b) arr=(1 2 3)
c) array arr 1 2 3
d) set arr = [1,2,3]

Question 6.2

How do you access the first element of array arr?
a) arr[1]
b) arr[0]
c) $arr[1]
d) ${arr[0]}

Question 6.3

What does ${#arr[@]} return?
a) Number of elements
b) Length of first element
c) All elements
d) Array indices

Question 6.4

How do you create an associative array?
a) arr=(key=value)
b) declare -A arr
c) array -A arr
d) map arr

Question 6.5

How do you loop through all array elements?
a) for i in arr
b) for i in ${arr}
c) for i in "${arr[@]}"
d) for each i in arr


Section 7: Control Flow

Question 7.1

What's the correct if statement syntax?
a) if [ $a -eq 5 ] then
b) if [ $a -eq 5 ]; then
c) if ($a == 5) then
d) if $a = 5 then

Question 7.2

What does -eq mean in Bash comparisons?
a) Equal to (for strings)
b) Equal to (for numbers)
c) End quote
d) Exit quietly

Question 7.3

How do you write a for loop that iterates 5 times?
a) for i in 1..5
b) for i in {1..5}
c) for (i=1; i<=5; i++)
d) loop i from 1 to 5

Question 7.4

What's the difference between while and until?
a) while runs while condition true, until runs until condition true
b) No difference
c) while is faster
d) until is for numbers only

Question 7.5

What does case $var in pattern matching do?
a) Checks variable type
b) Multi-way branch based on pattern
c) Converts variable case
d) Creates variable cases


Section 8: Functions

Question 8.1

How do you define a function in Bash?
a) function myfunc() { }
b) def myfunc() { }
c) func myfunc() { }
d) myfunc: function() { }

Question 8.2

How do you access function arguments?
a) $arg1, $arg2
b) $1, $2
c) $args[1], $args[2]
d) $ARGV[1]

Question 8.3

How do you return a value from a function?
a) return value
b) exit value
c) echo value and capture
d) result value

Question 8.4

What does local var=10 do inside a function?
a) Creates a global variable
b) Creates a local variable
c) Creates a readonly variable
d) Creates an exported variable

Question 8.5

How do you get the number of arguments passed to a function?
a) $#
b) $@
c) $*
d) $?


Section 9: Advanced Topics

Question 9.1

What does 2>&1 do in a command?
a) Redirects stderr to stdout
b) Redirects stdout to stderr
c) Redirects both to file
d) Creates two output files

Question 9.2

What's the purpose of a here document (<< EOF)?
a) Document code
b) Multi-line input
c) End of file marker
d) Error output

Question 9.3

What does trap command do?
a) Catches signals
b) Creates trap files
c) Debugs scripts
d) Traps errors

Question 9.4

What's process substitution <() used for?
a) Running commands in background
b) Using command output as file
c) Substituting processes
d) Creating subprocesses

Question 9.5

What does set -e do in a script?
a) Exit on error
b) Enable echo
c) Set environment
d) Enable debugging


Section 10: Practical Scenarios

Question 10.1

How would you find all .txt files in current directory?
a) find . -name "*.txt"
b) ls *.txt
c) grep .txt *
d) search txt

Question 10.2

How do you count lines in all .log files?
a) wc -l *.log
b) count lines *.log
c) cat *.log | wc -l
d) grep -c . *.log

Question 10.3

What command checks if a file exists?
a) test -f file
b) exists file
c) if file
d) check file

Question 10.4

How do you create a background process?
a) command &
b) background command
c) command bg
d) run command -b

Question 10.5

What does ps aux | grep bash do?
a) Shows bash processes
b) Kills bash processes
c) Starts new bash
d) Configures bash


Section 11: Debugging and Error Handling

Question 11.1

How do you enable debug mode in a bash script?
a) bash -d script.sh
b) bash -x script.sh
c) bash --debug script.sh
d) debug script.sh

Question 11.2

What does set -u do?
a) Treat unset variables as error
b) Set user permissions
c) Enable unicode
d) Set uppercase

Question 11.3

How do you check the exit status of the last command?
a) $?
b) $!
c) $@
d) $#

Question 11.4

What's the purpose of || in command1 || command2?
a) Run command1 then command2
b) Run command2 if command1 fails
c) Run both in parallel
d) Run command1 or command2

Question 11.5

What does trap 'cleanup' EXIT do?
a) Runs cleanup on script exit
b) Traps exit command
c) Exits on trap
d) Creates exit trap


Section 12: Regular Expressions

Question 12.1

Which grep option uses extended regular expressions?
a) -e
b) -E
c) -x
d) -r

Question 12.2

What does ^ mean in a regular expression?
a) Start of line
b) End of line
c) Any character
d) Not

Question 12.3

What does [0-9]+ match?
a) One or more digits
b) Zero or more digits
c) Exactly one digit
d) Digits 0-9

Question 12.4

How would you match an email address with regex?
a) [^@]+@[^.]+.com
b) .+@.+\..+
c) [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
d) All of the above patterns

Question 12.5

What does grep -v do?
a) Invert match (show non-matching lines)
b) Verbose output
c) Version info
d) Variable match


Section 13: Network Commands

Question 13.1

What does ping command test?
a) Network connectivity
b) Packet processing
c) Port availability
d) DNS resolution

Question 13.2

How do you copy files over SSH securely?
a) ssh copy
b) scp
c) netcp
d) remotecp

Question 13.3

What does curl do?
a) Transfer data from/to server
b) Check network curl
c) Create URL links
d) Curl files

Question 13.4

How do you check open ports on a remote host?
a) portscan
b) nc -zv host port
c) checkport
d) netport

Question 13.5

What does wget do?
a) Web get - download files
b) Wake get - wake on LAN
c) Web get - web server
d) Wait get - delayed download


Section 14: System Administration

Question 14.1

How do you view disk usage?
a) du
b) df
c) disk
d) space

Question 14.2

What's the difference between du and df?
a) du shows file/directory usage, df shows filesystem usage
b) No difference
c) du is for directories, df for files
d) du is older version

Question 14.3

How do you view running processes?
a) ps
b) proc
c) process
d) run

Question 14.4

What does kill -9 PID do?
a) Force kill process
b) Graceful termination
c) Pause process
d) Resume process

Question 14.5

How do you schedule a cron job?
a) crontab -e
b) schedule job
c) cron add
d) timed job


Section 15: Best Practices

Question 15.1

Why should you quote variables in Bash?
a) To handle spaces and special characters
b) To make them faster
c) To make them global
d) To prevent syntax errors

Question 15.2

What's the purpose of #!/bin/bash at the start of scripts?
a) Shebang - specifies interpreter
b) Comment
c) Header
d) Script name

Question 15.3

Why use set -e in production scripts?
a) Exit on error to prevent cascading failures
b) Enable error messages
c) Set environment
d) Enable logging

Question 15.4

How should you handle temporary files?
a) Create with mktemp and clean up with trap
b) Create in /tmp and leave
c) Create in current directory
d) Create with fixed names

Question 15.5

What's the recommended way to get user input?
a) read
b) input
c) get
d) prompt


Answer Key

Section 1: Basic Commands

1.1: a - pwd = Print Working Directory
1.2: b - ls = list
1.3: b - ~ is home directory
1.4: c - mkdir = make directory
1.5: b - rm = remove

Section 2: File Operations

2.1: b - Recursive copy for directories
2.2: b - mv = move
2.3: a - cp copies, mv moves/deletes original
2.4: a - List files then destination
2.5: b - Recursive force removal

Section 3: Text Processing

3.1: a - First 5 lines
3.2: b - tail shows end of file
3.3: b - grep searches for patterns
3.4: a - Global replacement
3.5: b - wc -l counts lines

Section 4: Permissions

4.1: b - 755 = rwxr-xr-x
4.2: a - chown = change owner
4.3: a - +x adds execute
4.4: b - Long listing with details
4.5: a - r--r--r-- = read for all

Section 5: Variables

5.1: b - No spaces around =
5.2: b - $ prefix to access
5.3: a - declare -i makes integer
5.4: a - $@ preserves array nature
5.5: a - readonly makes constant

Section 6: Arrays

6.1: b - arr=(values) syntax
6.2: d - ${arr[0]} for first element
6.3: a - ${#arr[@]} counts elements
6.4: b - declare -A for associative arrays
6.5: c - "${arr[@]}" preserves elements

Section 7: Control Flow

7.1: b - if [ condition ]; then
7.2: b - -eq for numeric equality
7.3: b - {1..5} brace expansion
7.4: a - while true, until false
7.5: b - case for pattern matching

Section 8: Functions

8.1: a - function name() { } syntax
8.2: b - $1, $2 for arguments
8.3: c - echo for data, return for status
8.4: b - local creates function-scoped variable
8.5: a - $# counts arguments

Section 9: Advanced

9.1: a - 2>&1 redirects stderr to stdout
9.2: b - Here document for multi-line input
9.3: a - trap catches signals
9.4: b - Process substitution as file
9.5: a - set -e exits on error

Section 10: Practical

10.1: a - find is recursive, ls is not
10.2: a - wc -l on multiple files
10.3: a - test -f checks file existence
10.4: a - & runs in background
10.5: a - Shows bash processes

Section 11: Debugging

11.1: b - bash -x for trace
11.2: a - set -u treats unset as error
11.3: a - $? holds last exit status
11.4: b - OR operator, runs on failure
11.5: a - trap runs on EXIT signal

Section 12: Regular Expressions

12.1: b - -E for extended regex
12.2: a - ^ anchors to start
12.3: a - + means one or more
12.4: c - Complete email pattern
12.5: a - -v inverts match

Section 13: Network

13.1: a - ping tests connectivity
13.2: b - scp = secure copy
13.3: a - curl transfers data
13.4: b - nc (netcat) checks ports
13.5: a - wget downloads files

Section 14: System Admin

14.1: b - df shows filesystem usage
14.2: a - du for files, df for filesystem
14.3: a - ps shows processes
14.4: a - kill -9 forces termination
14.5: a - crontab -e edits cron

Section 15: Best Practices

15.1: a - Quotes handle spaces/special chars
15.2: a - Shebang specifies interpreter
15.3: a - Exit on error prevents cascade
15.4: a - mktemp + trap for cleanup
15.5: a - read is standard input method


Scoring Guide

  • 90-100%: Bash Expert! You have comprehensive knowledge.
  • 75-89%: Advanced User - Solid understanding with room to grow.
  • 60-74%: Intermediate - Good foundation, need more practice.
  • 40-59%: Beginner - Keep learning and practicing.
  • Below 40%: Just starting - Review the basics and try again.

Common Mistakes to Review

If you missed questions in certain sections, focus on:

  • Commands 1-2: Basic navigation and file operations
  • Text Processing 3: grep, sed, awk fundamentals
  • Permissions 4: chmod, chown, octal notation
  • Variables 5: Declaration, scope, special variables
  • Arrays 6: Indexed vs associative, element access
  • Control Flow 7: if/then, loops, case statements
  • Functions 8: Definition, arguments, return values
  • Advanced 9-14: Redirection, signals, networking, system admin
  • Best Practices 15: Security, error handling, portability

Additional Practice

To improve your skills:

  1. Write scripts that combine multiple concepts
  2. Review man pages for unfamiliar commands
  3. Practice debugging with set -x
  4. Read other people's bash scripts
  5. Contribute to open-source shell scripts

Remember: Bash mastery comes from regular practice and real-world problem solving!

Leave a Reply

Your email address will not be published. Required fields are marked *


Macro Nepal Helper