Top 10 Linux Shell Interview Questions with Clear Answers
This article presents ten common Linux shell interview questions, each accompanied by concise explanations and practical command‑line examples, covering script termination, file header removal, line length checking, non‑printable character display, directory permissions, process states, and useful utilities like cut, cmp, diff, and echo.
1. How to abort a shell script before it finishes?
Use the exit command. When exit returns a non‑zero status (e.g., exit -1), the script terminates with an error. Example:
#!/bin/bash
echo "Hello"
exit -1
echo "bye"Running the script shows that everything before exit -1 executes, then the script stops.
2. How to delete the header line of a file?
The sed command removes specific lines. To delete the first line:
# sed '1 d' file.txtUsing the in‑place edit flag -i avoids the need for redirection:
# 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 5th line of linuxmi.txt:
# 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 listThis shows characters such as ^M (carriage return).
5. How to create a shared directory where members can create files but only owners can delete them?
Use the sticky bit on the directory:
# mkdir dir_xyz
# chmod g+wx dir_xyz
# chmod +t dir_xyzThe +t (sticky) permission 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 idle, awaiting resources.
Running – the process is actively executing.
Stopped – the process has been halted, either after completion or by a signal.
Zombie – the process has terminated but remains in the process table.
7. How to use the cut command?
cutextracts specific columns or character ranges from a file. Examples:
# cut -c1-10 txt_linuxmi # first 10 characters of each line
# cut -d';' -f2 -f5 -f7 txt_linuxmi # fields 2, 5, and 7 using ';' as delimiter8. What is the difference between cmp and diff ?
cmpcompares two files byte by byte and reports the first mismatch. diff produces a list of changes needed to make the files identical, showing line‑by‑line differences.
9. Can echo replace ls ?
Yes. echo * lists the same filenames that ls would display, 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 a unique inode number that identifies the file within the filesystem.
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.
