Essential Linux Shell Interview Questions and Practical Answers
This guide provides concise explanations and command examples for common Linux shell interview topics, covering script termination, file header removal, line length checking, displaying non‑printable characters, directory permissions, process states, and useful text‑processing commands.
Linux offers a vast landscape for developers, and mastering common shell tasks is valuable for both career growth and knowledge expansion. Below are practical interview‑style questions with clear answers and command examples.
1. How to abort a shell script before it finishes?
Use the exit command. Exiting with a non‑zero status (e.g., exit -1) causes the script to terminate with an error. Example script:
#!/bin/bash
echo "Hello"
exit -1
echo "bye"Running it yields:
# sh linuxmi.sh
Hello
linuxmi.sh: line 3: exit-1: command not found
bye2. How to remove the header line of a file?
The sed command can delete specific lines. To delete the first line: sed '1 d' file.txt With the -i option you can edit the file in place:
sed -i '1 d' file.txt3. How to check the length of a specific line in a text file?
Combine sed to select the line and pipe to wc -c to count characters. Example for the fifth line:
sed -n '5 p' linuxmi.txt | wc -c4. How to display all non‑printable characters in a file?
Open the file in vi, enter command mode with :, and run: set list This shows characters such as ^M.
5. How to create a shared directory where members can create files but only owners can delete them?
Use the sticky bit:
# mkdir dir_xyz
# chmod g+wx dir_xyz
# chmod +t dir_xyzThe +t (sticky) bit ensures only the file owner, directory owner, or root can delete files.
6. What are the main lifecycle stages of a Linux process?
Waiting – the process is waiting for resources.
Running – the process is actively executing.
Stopped – the process has finished or received a termination signal.
Zombie – the process has terminated but remains in the process table.
7. How to use the cut command?
cutextracts sections of each line of a file. Examples:
# cut -c1-10 txt_linuxmi # first 10 characters
# cut -d';' -f2 -f5 -f7 txt_linuxmi # fields 2, 5, and 7 using ';' as delimiter8. Difference between cmp and diff ?
cmpcompares two files byte‑by‑byte and reports the first mismatch. diff shows the changes needed to make the files identical, providing a more readable diff output.
9. Can echo replace ls ?
Yes, echo * lists the same filenames as ls in the current directory, though formatting differs.
10. What is an inode?
An inode is a data structure on Linux/Unix file systems that stores metadata about a file, including its unique inode number.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
