Top 10 Linux Shell Interview Questions and Practical Answers
This article presents ten common Linux shell interview questions covering script termination, file header removal, line length checking, viewing non‑printable characters, directory permissions with sticky bits, process states, cut, cmp vs diff, using echo instead of ls, and a brief explanation of inodes, each accompanied by clear command‑line examples.
Linux offers a vast landscape for learning useful shell commands that can boost both career and knowledge.
1. How to interrupt a shell script before it finishes?
Use the exit command. Providing a non‑zero status, such as exit -1, forces the script to terminate with an error.
#!/bin/bash
echo "Hello"
exit -1
echo "bye"Running the script shows the exit command stops execution before the final echo.
# sh linuxmi.sh
Hello
linuxmi.sh: line 3: exit-1: command not found
bye2. How to remove the header (first line) of a file?
The sed command can delete the first line:
# sed '1 d' file.txtUsing the -i option edits 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 with wc:
# sed -n '5 p' linuxmi.txt | wc -cAn example command to get the length of line 5:
# sed -n '5 p' linuxmi.txt | wc -c4. How to view all non‑printable characters in Linux?
Open vi, press Esc, then type :set list. This displays characters such as ^M.
Note: This method shows all non‑printable characters, including Ctrl+M (^M).
5. How to create a shared directory where group members can create files but only the owner can delete them?
# 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 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 the first 10 columns of a file:
# cut -c1-10 txt_linuxmiExtract specific fields (e.g., 2, 5, and 7) using a delimiter:
# cut -d';' -f2 -f5 -f7 txt_linuxmi8. 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.
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 to uniquely identify a file; each file has its own 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.
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.
