Top 10 Linux Shell Interview Questions and How to Solve Them
This article walks through ten common Linux shell interview questions, covering script interruption, file header removal, line length checking, viewing non‑printing characters, directory permissions, process states, cut usage, cmp vs diff, echo vs ls, and a brief explanation of inodes.
1. How to interrupt a shell script before it finishes? Use the
exitcommand; exiting with a non‑zero status (e.g.,
exit -1) causes the script to terminate with an error.
<code>#!/bin/bash
echo "Hello"
exit -1
echo "bye"
</code>Running the script shows the error at the
exit -1line.
2. How to remove the header (first line) of a file? The
sedcommand can delete the first line:
sed '1 d' file.txt. Using the in‑place option simplifies it:
sed -i '1 d' file.txt.
3. How to check the length of a specific line in a text file? Combine
sedwith
wc, e.g.,
sed -n '5 p' linuxmi.txt | wc -cto get the character count of line 5.
4. How to view all non‑printing characters in a file? Open the file in
vi, enter command mode with
:, and run
set list. This displays characters such as
^M.
5. Create a shared directory where members can create or access files but cannot delete others' files. Use the sticky bit:
<code># mkdir dir_xyz
# chmod g+wx dir_xyz
# chmod +t dir_xyz
</code>The +t (sticky) bit ensures only the file owner, directory owner, or root can delete files. 6. What are the main 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 kill signal.
Zombie – the process has terminated but remains in the process table.
7. How to use the cut command? Extract columns or character ranges, e.g., cut -c1-10 txt_linuxmi for the first ten characters, or cut -d';' -f2 -f5 -f7 txt_linuxmi for specific fields. 8. Difference between cmp and diff ? cmp compares files byte‑by‑byte and reports the first mismatch, while diff shows the changes needed to make the files identical. 9. Can echo replace ls ? Yes; echo * lists directory contents similarly to ls . 10. What is an inode? An inode is a data structure used by Linux/Unix to store metadata about a file; each file has a unique inode number.
Efficient Ops
This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.
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.