Bash Scripting Interview Question and Answers
Getting Familiar with Bash:
Q: What is the role of Bash in a Linux environment?
A: Bash is a command interpreter, or shell. It provides a command line user interface for controlling your computer (or Unix computer). It is also a powerful scripting language, as it can combine multiple commands into one script, making it useful for automating tasks.
Q: How do you redirect STDOUT and STDERR in Bash?
A: You can redirect STDOUT using the “>” operator and STDERR using the “2>” operator. For example, command > file.txt redirects STDOUT to file.txt and command 2> error.txt redirects STDERR to error.txt.
Learning Linux Essentials for Shell Scripting:
Q: How do you use the grep command with regular expressions?
A: The grep command can be used with regular expressions to search for patterns within files. For example, grep “^test” file.txt will find lines in file.txt that start with “test”.
Q: What is the purpose of sed and awk commands?
A: sed is a stream editor for filtering and transforming text.
awk is a programming language used for text processing. Both are powerful tools for manipulating text in shell scripts.
Working with Variables and Arguments:
Q: How do you define and use variables in Bash?
A: Variables in Bash can be defined using the “=” operator with no spaces around it. For example, VAR=”Hello World” defines a variable VAR with the value “Hello World”. You can use the variable with the “$” symbol, like echo $VAR.
Transforming Input:
Q: How do you use the tr command?
A: The tr command is used for translating or deleting characters. For example, echo “HELLO” | tr ‘A-Z’ ‘a-z’ will output “hello”.