Professional BASH (Bourne Again Shell) 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 (Bourne Again Shell). Whether you are preparing for a technical interview or just testing your knowledge, these FAQ Questions will help you succeed.

What is BASH?

BASH (Bourne Again Shell) is a Unix shell and command language used for scripting and system administration.

Who developed BASH?

BASH was developed by the GNU Project as a free software replacement for the Bourne Shell (sh).

How do you create a variable in BASH?

Using VAR_NAME=value, e.g., name="John".

How do you print a variable in BASH?

Using echo, e.g., echo $name.

What is the difference between $VAR and ${VAR}?

Both reference a variable, but ${VAR} helps avoid ambiguity.

How do you get user input in BASH?

Using read, e.g., read name; echo "Hello, $name".

How do you define a function in BASH?

Using function myFunc() { commands; } or myFunc() { commands; }.

How do you call a function in BASH?

Simply by using the function name, e.g., myFunc.

How do you execute a BASH script?

Using bash script.sh or ./script.sh (if executable).

How do you make a script executable?

Using chmod +x script.sh.

What is the shebang (#!) in BASH?

It specifies the interpreter, e.g., #!/bin/bash at the start of the script.

How do you use command-line arguments in BASH?

Using $1, $2, etc., e.g., echo "First arg: $1".

What does $@ and $* do in BASH?

Both represent all command-line arguments, but $@ preserves arguments as separate words.

How do you check if a command was successful?

Using $?, where 0 means success.

How do you use an if statement in BASH?

if [ condition ]; then commands; fi.

How do you check if a file exists?

Using [ -f filename ], e.g., if [ -f myfile ]; then echo "Exists"; fi.

How do you check if a directory exists?

Using [ -d dirname ], e.g., if [ -d mydir ]; then echo "Exists"; fi.

How do you use a for loop in BASH?

for i in 1 2 3; do echo $i; done.

How do you use a while loop in BASH?

while [ condition ]; do commands; done.

How do you use a case statement in BASH?

case $var in pattern1) commands;; pattern2) commands;; esac.

What is the difference between = and == in BASH?

= is for assignment, while == is used in string comparison.

How do you redirect output to a file?

Using command > file (overwrite) or command >> file (append).

How do you redirect errors to a file?

Using command 2> file or command 2>> file.

How do you redirect both output and error to a file?

Using command > file 2>&1.

How do you list all environment variables?

Using printenv or env.

How do you export a variable in BASH?

Using export VAR=value.

What is the difference between local and export in BASH?

local defines variables within a function, while export makes them available to child processes.

How do you find the path of a command?

Using which command or type command.

How do you check the number of arguments passed?

Using $#, e.g., echo "Arguments: $#"

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

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

What does set -e do in a script?

It makes the script exit immediately on error.

What does set -x do in a script?

It enables debugging by printing each command before executing it.

How do you execute a command stored in a variable?

Using eval, e.g., cmd="ls -l"; eval $cmd.

How do you run a command in the background?

Using command &.

How do you bring a background job to the foreground?

Using fg.

How do you kill a process in BASH?

Using kill PID or pkill process_name.

How do you find all running processes?

Using ps aux or top.

What is the difference between > and >>?

> overwrites a file, while >> appends to it.

How do you check disk usage?

Using df -h or du -sh folder_name.

How do you check memory usage?

Using free -h.

What does && and `

How do you search for a string in a file?

Using grep "pattern" filename.

How do you replace text in a file?

Using sed, e.g., sed -i 's/old/new/g' file.txt.

How do you count lines in a file?

Using wc -l filename.

How do you list only directories?

Using ls -d */.

How do you find the current shell?

Using echo $SHELL.

How do you check if a user is root?

Using if [ $(id -u) -eq 0 ]; then echo "Root user"; fi.

How do you schedule a cron job?

Using crontab -e and adding * * * * * command.

How do you create an alias?

Using alias name='command', e.g., alias ll='ls -la'.

How do you remove an alias?

Using unalias name, e.g., unalias ll.