Top 10 Linux Shell Interview Questions with Clear Answers
This article presents ten common Linux shell interview questions, explains how to interrupt scripts, manipulate files with sed, check line lengths, view non‑printable characters, set directory permissions, understand process states, and use commands like cut, cmp, diff, echo, and inode.
This guide answers ten frequently asked Linux shell interview questions, providing concrete command examples and explanations.
1. How to abort a shell script before it finishes?
Use the exit command with a non‑zero status. For example, placing exit -1 in the script will cause it to terminate with an error.
#!/bin/bash
echo "Hello"
exit -1
echo "bye"Running the script shows the error message and the subsequent echo "bye" still executes because the typo exit-1 is not recognized.
2. How to remove the header line of a file?
The sed command can delete the first line. To save the result, redirect the output or use the -i in‑place option.
# sed '1d' file.txt > new_file.txt
# sed -i '1d' file.txt3. How to check the length of a specific line in a text file?
Combine sed to print the line and pipe to wc -c to count characters.
# sed -n '5p' linuxmi.txt | wc -c4. How to display all non‑printable characters in a file?
Open the file in vi, enter command mode, and execute set list. This reveals characters such as ^M.
5. How to create a shared directory where members can create files but only the owner can delete them?
Use the sticky bit and group write/execute permissions:
# mkdir dir_xyz
# chmod g+wx dir_xyz
# chmod +t dir_xyzThe sticky bit ( +t) ensures only the file owner, directory owner, or root can delete files.
6. What are the main stages of a Linux process lifecycle?
Waiting : The process is waiting for resources.
Running : The process is actively executing.
Stopped : The process has been halted or received a kill 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.
# cut -c1-10 txt_linuxmi # first 10 characters
# cut -d';' -f2,5,7 txt_linuxmi # fields 2, 5, and 7 using ';' as delimiter8. Difference between cmp and diff ?
diffshows the changes needed to make two files identical, while cmp compares files byte by byte and reports the first mismatch.
9. Can echo replace ls ?
Yes, echo * lists directory contents similarly to ls, though formatting differs.
10. What is an inode?
An inode is a data structure on a Unix/Linux filesystem that stores metadata about a file, including 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.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
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.
