Linux and Bash Scripting - Interview Questions and Answers
Linux Interview Questions and Answers
1. What is the difference between `grep` and `egrep`?
Answer: Both `grep` and `egrep` are used to search for text within files. The main difference is that `egrep` is an extended version of `grep` that supports extended regular expressions (ERE), while `grep` supports basic regular expressions (BRE). In `grep`, you need to escape certain special characters to use them as literals, while `egrep` provides more powerful pattern matching without needing such escapes.
2. How would you find the number of lines in a file?
Answer: You can use the `wc` (word count) command with the `-l` option to count the number of lines in a file. For example: wc -l filename
.
3. What is the purpose of the `chmod` command?
Answer: The `chmod` command is used to change the permissions of a file or directory. It can set read, write, and execute permissions for the owner, group, and others. For example: chmod 755 filename
sets the permissions to read, write, and execute for the owner, and read and execute for the group and others.
4. Explain the difference between `&&` and `||` in Bash scripting.
Answer: In Bash, `&&` is a logical AND operator that executes the second command only if the first command succeeds (returns a status code of 0). For example: command1 && command2
. On the other hand, `||` is a logical OR operator that executes the second command only if the first command fails (returns a status code other than 0). For example: command1 || command2
.
5. How do you find the disk usage of a directory?
Answer: Use the `du` command to find the disk usage of a directory. For example: du -sh /path/to/directory
will display the size of the directory in a human-readable format.
6. What does `ps aux` command do?
Answer: The `ps aux` command displays a snapshot of all current processes running on the system. The `a` option shows processes for all users, `u` provides user-oriented format, and `x` includes processes without a controlling terminal.
7. How would you search for a specific string in a file?
Answer: Use the `grep` command to search for a specific string in a file. For example: grep "search_string" filename
.
8. What is the purpose of the `find` command?
Answer: The `find` command is used to search for files and directories in a directory hierarchy based on various criteria such as name, size, type, and modification time. For example: find /path/to/search -name "filename"
.
9. How do you redirect output to a file?
Answer: To redirect the output of a command to a file, use the `>` operator. For example: command > file.txt
will overwrite the contents of `file.txt` with the output of `command`. To append the output to an existing file, use the `>>` operator.
10. What is a symbolic link?
Answer: A symbolic link (symlink) is a type of file that points to another file or directory. It acts as a shortcut and allows you to access the target file or directory from a different location. Create a symlink using the `ln -s` command. For example: ln -s /path/to/original /path/to/symlink
.
11. What does the `diff` command do?
Answer: The `diff` command compares two files line by line and displays the differences between them. It can be used to find out what has changed between two versions of a file.
Bash Scripting Interview Questions and Answers
1. How do you declare a variable in Bash?
Answer: Declare a variable in Bash by simply assigning a value to it without spaces around the equals sign. For example: variable_name=value
. To use the variable, prefix it with a dollar sign: $variable_name
.
2. What is the purpose of the `shebang` line in a Bash script?
Answer: The `shebang` line (e.g., #!/bin/bash
) at the beginning of a script specifies the interpreter to be used to execute the script. It allows the script to be run as an executable file without explicitly invoking the interpreter.
3. How do you create a function in a Bash script?
Answer: Define a function in Bash using the following syntax:
function_name() {
# commands
}
Or, alternatively:
function function_name {
# commands
}
Call the function by simply typing its name: function_name
.
4. How do you handle command-line arguments in a Bash script?
Answer: Access command-line arguments using positional parameters: $1
, $2
, etc. For example, if you run a script with ./script.sh arg1 arg2
, you can access arg1
with $1
and arg2
with $2
. The special variable $#
gives the number of arguments, and $@
provides all arguments as a list.
5. What is the purpose of `set -e` in a Bash script?
Answer: The `set -e` command in a Bash script makes the script exit immediately if any command returns a non-zero exit status (indicating an error). It is used to ensure that the script stops execution when an error occurs, which helps in debugging and prevents the execution of subsequent commands that may depend on the success of the failed command.
6. How do you loop through a list of items in Bash?
Answer: Use a `for` loop to iterate through a list of items. For example:
for item in item1 item2 item3; do
echo "$item"
done
Alternatively, you can use a loop to iterate over files in a directory:
for file in /path/to/files/*; do
echo "$file"
done
7. How do you check if a file exists in a Bash script?
Answer: Use the `-e` option with an `if` statement to check if a file exists. For example:
if [ -e filename ]; then
echo "File exists"
else
echo "File does not exist"
fi
8. What does the `exit` command do in a Bash script?
Answer: The `exit` command terminates the execution of a Bash script and optionally returns an exit status to the calling process. For example, exit 0
indicates successful completion, while exit 1
indicates an error.
9. How do you perform string operations in Bash?
Answer: Bash provides various string operations, including:
- Concatenation:
string1="Hello"; string2="World"; result="$string1 $string2"
- Substring extraction:
substring=${string:position:length}
- String length:
length=${#string}
- Replace substring:
new_string=${string/old/new}
10. How do you handle errors in Bash scripting?
Answer: You can handle errors in Bash scripting using various techniques:
- Check the exit status of commands using
$?
. - Use `set -e` to exit the script on error.
- Implement error handling functions and conditional statements.
Multiple Choice Questions (MCQs)
1. Which command is used to view the first few lines of a file?
A) `tail
B) `head
C) `cat
D) `more
Answer: B) `head
2. What is the default file permission when a file is created?
A) 644
B) 666
C) 755
D) 777
Answer: B) 666
3. How do you make a script executable?
A) `chmod +x script.sh
B) `chmod 755 script.sh
C) `chown +x script.sh
D) `chmod 777 script.sh
Answer: A) `chmod +x script.sh
4. Which of the following is used to test conditions in Bash?
A) `test
B) `check
C) `verify
D) `cond
Answer: A) `test
5. What is the purpose of the `source` command in Bash?
A) To execute a script in a subshell
B) To run a script in the current shell
C) To load a module
D) To debug a script
Answer: B) To run a script in the current shell
6. What does `$(command)` do in Bash?
A) Executes `command` and returns its exit status
B) Executes `command` and returns its output
C) Runs `command` in the background
D) Starts `command` in a new shell
Answer: B) Executes `command` and returns its output
7. What will the command `echo $((2+3))` output?
A) 23
B) 5
C) 2+3
D) 2 3
Answer: B) 5
8. What is the function of the `mkdir` command?
A) To delete a directory
B) To rename a directory
C) To create a new directory
D) To list directory contents
Answer: C) To create a new directory
9. How do you concatenate two files in Unix?
A) `concat file1 file2
B) `join file1 file2
C) `cat file1 file2
D) `merge file1 file2
Answer: C) `cat file1 file2
10. What does the `grep -i` option do?
A) Searches for an exact match
B) Ignores case when searching
C) Searches for a pattern in binary files
D) Shows line numbers with matches
Answer: B) Ignores case when searching