Professional BASH Shell Scripting FAQ Questions and Answers

Welcome to our comprehensive Trivia Quiz and interview preparation guide. Below you will find a curated list of popular Trivia Questions and Answers specifically for BASH Shell Scripting. Whether you are preparing for a technical interview or just testing your knowledge, these FAQ Questions will help you succeed.

How do you define a variable in Bash?

var_name="value" (no spaces around =).

How do you access a variable in Bash?

Use $var_name or ${var_name}.

How do you take user input in Bash?

read variable_name.

How do you pass arguments to a Bash script?

Use $1, $2, ..., $n for positional arguments.

How do you print output in Bash?

Use echo "Hello" or printf "Hello\n".

How do you check the number of arguments passed to a script?

Use $# to get the count.

How do you write an if statement in Bash?

if [ condition ]; then commands; fi

How do you check if a file exists in Bash?

if [ -f filename ]; then echo "Exists"; fi

How do you check if a directory exists in Bash?

if [ -d dirname ]; then echo "Exists"; fi

How do you use an else statement in Bash?

if [ condition ]; then commands; else other_commands; fi

How do you write an elif statement in Bash?

if [ condition ]; then commands; elif [ condition ]; then commands; fi

What is the difference between [ ] and [[ ]] in Bash?

[[ ]] supports advanced pattern matching and logical operations.

How do you perform arithmetic operations in Bash?

Use expr, $(( expression )), or let.

How do you use a for loop in Bash?

for i in {1..5}; do echo $i; done

How do you use a while loop in Bash?

while [ condition ]; do commands; done

How do you break out of a loop in Bash?

Use break.

How do you skip an iteration in a loop?

Use continue.

How do you define a function in Bash?

function_name() { commands; }

How do you call a function in Bash?

function_name

How do you return a value from a function?

Use return or echo and capture it with $( ).

How do you get the exit status of a command?

Use $? (0 for success, nonzero for failure).

How do you make a script executable?

chmod +x script.sh

How do you append text to a file?

echo "text" >> file.txt

How do you write text to a file (overwrite)?

echo "text" > file.txt

How do you read a file line by line in Bash?

while read line; do echo "$line"; done < file.txt

How do you check if a command succeeded?

if command; then echo "Success"; fi

How do you find the length of a string in Bash?

echo ${#var}

How do you extract a substring in Bash?

${var:start:length}

How do you replace text in a variable?

echo ${var/old/new}

How do you convert a string to uppercase in Bash?

echo ${var^^}

How do you convert a string to lowercase in Bash?

echo ${var,,}

How do you list all environment variables?

Use env or printenv.

How do you export a variable in Bash?

Use export VAR_NAME=value.

How do you check if a command exists in Bash?

command -v command_name

How do you check disk usage in Bash?

Use df -h.

How do you check memory usage in Bash?

Use free -m.

How do you schedule a script to run later?

Use cron (crontab -e).

How do you find and delete files older than 7 days?

find /path -type f -mtime +7 -delete

How do you kill a process in Bash?

Use kill PID or pkill process_name.

How do you display running processes?

Use ps aux or top.

How do you check open network ports?

Use netstat -tulnp or ss -tulnp.

How do you extract the filename from a path?

basename /path/to/file.txt

How do you extract the directory from a path?

dirname /path/to/file.txt

How do you rename a file in Bash?

mv oldname newname

How do you compress a file in Bash?

Use tar -czvf archive.tar.gz file

How do you extract a .tar.gz file?

tar -xzvf archive.tar.gz

How do you print system uptime?

Use uptime.

How do you get the current date and time?

Use date or date +"%Y-%m-%d %H:%M:%S".

What is Bash?

Bash (Bourne Again Shell) is a command-line interpreter and scripting language for Unix/Linux.

How do you execute a Bash script?

Use bash script.sh or chmod +x script.sh && ./script.sh.