# Bash/Linux Interview Questions for DevOps Engineers

Bash scripting is a superpower for engineers. Whether automating repetitive tasks, gluing together tools, or managing systems, Bash is always there, simple yet powerful.&#x20;

<figure><img src="https://cdn-images-1.medium.com/max/800/1*alB08kNzKDCHdveX38htHw.jpeg" alt=""><figcaption><p>Bash</p></figcaption></figure>

**1. What is the purpose of the shebang (`#!`) line?**

It defines the interpreter that should execute the script. Example:

```
#!/bin/bash
```

**2. How do you read command-line arguments in a shell script?**

```
#!/bin/bash
echo "Script Name: $0"
echo "First Argument: $1"
echo "Total Arguments: $#"
echo "All Arguments: $@"
echo "Process ID: $$"
echo "Exit Status: $?"
```

**3. What are the standard streams in Linux?**

* **Standard Input (`stdin`)**
* **Standard Output (`stdout`)**
* **Standard Error (`stderr`)**

**4. What are the various types of variables used in shell scripting?**

There are two types of variables used in shell scripting:

* **System-generated variables**: These are created by the OS and can be viewed using the `set` command.
* **User-defined variables**: Created by users and accessed using the `echo` command.

**5. How do you check if a file exists using a conditional statement?**

```
if [ -f /var/log/messages ]; then echo "File exists"; fi
```

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

```
find /path/to/directory -type f -mtime +7 -exec rm -f {} \;
```

**7. What is `crontab`?**

`crontab` (CRON Table) is used to schedule jobs to run at specific times. It allows automation of system tasks.

**8. How do you debug a shell script?**

* Use `set -x` to enable debugging.
* Use `echo` statements to track variable values and execution flow.

**9. How do you rename all `.txt` files to `.log` in a directory?**

```
for file in *.txt; do mv "$file" "${file%.txt}.log"; done
```

**10. What are the default permissions of a file when it is created?**

The default permissions are determined by the `umask` value. Typically, a new file has `rw-rw-r--` (664) permissions.

**11. What are the four stages of a Linux process?**

* **Waiting**: Process is waiting for a resource.
* **Running**: Process is currently executing.
* **Stopped**: Process execution has been stopped.
* **Zombie**: Process has finished but still exists in the process table.

**12. What is a metacharacter?**

A metacharacter is a special character in the shell used for pattern matching. Example:

```
ls s*
```

Lists all files starting with ‘s’.

**13. Difference between `$@` and `$*`?**

* `$*` treats all arguments as a single string.
* `$@` treats each argument as a separate entity.

**14. How would you check if a file named `example` exists in a specific path?**

```
[ -e example ] && echo "File exists" || echo "File does not exist"
```

You can also use:

```
test -e example && echo "File exists" || echo "File does not exist"
```

**15. How would you check disk usage using shell commands?**

* `df -h`: Shows available disk space.
* `du -sh /data01/*`: Shows disk usage per directory.
* `dfspace`: Provides space usage in megabytes.

**16. What is the special shell variable `$?`?**

It stores the exit status of the last executed command.

```
cp file1 file2
if [ $? -eq 0 ]; then echo "Copy successful"; else echo "Copy failed"; fi
```

**17. How to extract a substring from a variable?**

```
variable="Her name is Jen, and she is a developer."
echo ${variable:12:6}  # Output: Jen, a
```

**18. Difference between `[[ $string == "efg*" ]]` and `[[ $string == efg* ]]`?**

* `[[ $string == efg* ]]` checks if `$string` starts with "efg".
* `[[ $string == "efg*" ]]` checks if `$string` is exactly "efg\*".

**19. How to list only filenames in a directory?**

```
ls -1
```

To store the output in a variable:

```
filelist=$(ls -1 /somedir/)
```

**20. What is the difference between soft and hard links?**

* **Soft link (symbolic link)**: A pointer to the original file.
* **Hard link**: A duplicate entry pointing to the same inode as the original file.

```
ln -s original_file soft_link
ln original_file hard_link
```

**21. How do you replace a string in multiple files using `sed`?**

```
sed -i 's/old_string/new_string/g' *.txt
```

**22. What does `grep -oP '\\d+'` do?**

It extracts numeric values from a string.

```
echo "Version=RevNo.201963" | grep -oP '\\d+'
```

Output: `201963`

These questions cover key Bash and Linux concepts that every DevOps engineer should be familiar with. Mastering these will help you ace your next interview!

Happy scripting! 🖥️
