Top 10 Linux Shell Interview Questions and Practical Answers
This article provides concise, step‑by‑step solutions to ten common Linux shell interview questions, covering script termination, file header removal, line length checking, displaying non‑printable characters, sticky‑bit directories, process states, cut usage, cmp vs diff, echo vs ls, and a brief explanation of inodes.
1. How to abort a shell script?
Use exit with a non‑zero status, e.g., exit -1. When a script encounters a non‑zero exit code it terminates.
#!/bin/bash
echo "Hello"
exit -1
echo "bye"Running the script shows that execution stops before the second echo.
2. How to remove the header line of a file?
Use sed to delete the first line: sed '1d' file.txt. With the -i option you can edit the file in place: sed -i '1d' file.txt.
3. How to check the length of a specific line in a text file?
Combine sed to print the desired line and pipe to wc -c to count bytes, e.g.:
# sed -n '5p' linuxmi.txt | wc -c4. How to display all non‑printable characters in a file?
In vi (or vim) enable the list option: press Esc, type :set list and press Enter. This shows characters such as ^M.
5. How to create a shared directory where only the file creator can delete files?
Use the sticky bit:
# mkdir dir_xyz
# chmod g+wx dir_xyz
# chmod +t dir_xyzThe +t (sticky) bit allows only the file’s owner, the directory’s owner, or root to delete files.
6. What are the four main states of a Linux process?
Waiting – the process is waiting for resources.
Running – the process is currently executing.
Stopped – the process has been stopped by a signal or after completion.
Zombie – the process has terminated but still has an entry in the process table.
7. How to use the cut command?
cutextracts selected columns or fields. Examples:
# cut -c1-10 txt_linuxmi # first 10 characters of each line
# cut -d';' -f2,5,7 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 produces a line‑by‑line description of changes needed to make the files identical.
9. Can echo replace ls ?
Yes, echo * lists the same filenames as ls in the current directory, though it lacks formatting options.
10. What is an inode?
An inode is a data structure that stores metadata about a file in Unix‑like systems; each file has a 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.
