Top 10 Linux Shell Interview Questions and Practical Answers
This article presents ten common Linux shell interview questions, providing clear explanations, command-line solutions, code examples, and step‑by‑step guidance on script termination, file header removal, line length checking, non‑printable character display, directory permissions, process states, cut, cmp vs diff, echo vs ls, and inode basics.
1. Interrupting a Shell Script
Use the exit command with a non‑zero status to stop a script. For example, exit -1 forces an error and terminates execution.
Example script:
#!/bin/bash
echo "Hello"
exit -1
echo "bye"Running sh linuxmi.sh produces:
Hello
linuxmi.sh: line 3: exit-1: command not found
bye2. Removing the First Line of a File
The sed command can delete the header line:
sed '1 d' file.txtTo save the result, redirect the output:
sed '1 d' file.txt > new_file.txtOr edit the file in place with the -i option:
sed -i '1 d' file.txt3. Checking the Length of a Specific Line
Combine sed with wc -c to count characters:
sed -n '5 p' linuxmi.txt | wc -c4. Viewing All Non‑Printable Characters
Open a file in vi, enter command mode with Esc then :, and run:
set listThis displays characters such as ^M for carriage returns.
5. Directory Permissions for Group Collaboration
Create the directory and set group write/execute and the sticky bit so only owners can delete files:
mkdir dir_xyz
chmod g+wx dir_xyz
chmod +t dir_xyzThe sticky bit ( +t) ensures that only the file owner, directory owner, or root can remove files.
6. Linux Process Lifecycle
Waiting : Process is waiting for resources.
Running : Process is actively executing.
Stopped : Process has been halted or killed.
Zombie : Process has terminated but remains in the process table.
7. Using the cut Command
Extract the first 10 characters of a file:
cut -c1-10 txt_linuxmiExtract specific columns (e.g., 2, 5, and 7) using a delimiter:
cut -d';' -f2 -f5 -f7 txt_linuxmi8. 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. Replacing ls with echo
Listing directory contents can be mimicked by echo *, which outputs the same file names as ls.
10. What Is an Inode?
An inode is a data structure used by Linux to store metadata about a file, including a unique inode number that identifies the file within the filesystem.
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.
