Top 10 Linux Shell Interview Questions with Ready‑to‑Use Answers
This article presents ten common Linux shell interview questions, each followed by clear explanations and exact command‑line solutions—including script termination, file header removal, line length checking, non‑printable character display, directory permissions, process states, cut usage, cmp vs diff, echo vs ls, and a brief overview of inodes.
1. Abort a shell script before it finishes
Use exit with a non‑zero status. The script stops immediately and returns the given status to the shell.
#!/bin/bash
echo "Hello"
exit -1 # non‑zero status aborts the script
echo "bye" # this line is never executedRunning the script prints only the lines before exit and returns the error code.
2. Remove the header (first line) of a file
The sed stream editor can delete a specific line. To drop the first line and write the result to standard output: sed '1d' file.txt To save the output to a new file, redirect the stream: sed '1d' file.txt > new_file.txt For an in‑place edit (modify the original file), use the -i option:
sed -i '1d' file.txt3. Check the length of a specific line in a text file
Combine sed with wc -c (byte count). The command below prints the number of bytes in line n of file.txt: sed -n 'n p' file.txt | wc -c Example – length of the 5th line in linuxmi.txt:
sed -n '5 p' linuxmi.txt | wc -c4. View all non‑printable characters in a file
Open the file with vi (or vim) and enable the list option:
Start vi on the target file.
Press Esc to ensure you are in command mode.
Type :set list and press Enter.
The editor displays invisible characters such as ^M (carriage return) and end‑of‑line symbols.
5. Create a shared directory where members can create files but only owners can delete them
Set the sticky bit on the directory. The typical steps are:
mkdir dir_xyz # create the directory
chmod g+wx dir_xyz # give the group write and execute permissions
chmod +t dir_xyz # set the sticky bit ("+t")With the sticky bit, a file can be removed only by its owner, the directory owner, or the super‑user.
6. Stages of a Linux process
Waiting – the process is blocked, waiting for resources (e.g., I/O, CPU).
Running – the scheduler has assigned CPU time and the process is executing.
Stopped – the process has been paused, either because it finished a job or received a signal such as SIGSTOP or SIGTSTP.
Zombie – the process has terminated but its exit status has not yet been collected by the parent, so it remains in the process table.
7. Using the cut command
Extract the first 10 characters (columns) of each line in txt_linuxmi: cut -c1-10 txt_linuxmi Extract specific fields when the file uses a custom delimiter (semicolon in this example). The -f option can be repeated or combined with commas:
cut -d';' -f2,5,7 txt_linuxmi8. Difference between cmp and diff
diffproduces a human‑readable description of the changes required to make two files identical (line‑oriented). cmp performs a byte‑by‑byte comparison and reports the first mismatching byte offset, which is useful for binary files.
9. Can echo replace ls ?
For a simple listing of file names in the current directory, echo * expands the wildcard and prints the same names that ls would show. However, ls provides additional formatting, sorting options, and metadata that echo does not.
10. What is an inode?
An inode is a filesystem data structure that stores metadata about a file (owner, permissions, timestamps, size, block locations, etc.). Each file is identified by a unique inode number within its filesystem; the number is used by the kernel to locate the file’s data.
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.
