Operations 8 min read

Top 10 Linux Shell Interview Questions and How to Solve Them

This article presents ten common Linux shell interview questions, providing clear explanations and step‑by‑step command examples—including script termination, file header removal, line length checking, non‑printable character display, directory permissions, process states, and differences between utilities—so readers can master essential command‑line techniques.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Top 10 Linux Shell Interview Questions and How to Solve Them

1. How to abort a shell script before it finishes?

Use the exit command with a non‑zero status; for example, exit -1 forces the script to terminate with an error.

Example script linuxmi.sh:

#!/bin/bash

echo "Hello"
exit-1

echo "bye"

Running sh linuxmi.sh produces:

Hello
linuxmi.sh: line 3: exit-1: command not found
bye

The typo exit-1 is not a valid command, so the script continues after reporting the error.

2. How to remove the header (first line) of a file?

The sed command can delete specific lines. To drop the first line: sed '1 d' file.txt To save the result back to a file, redirect the output: sed '1 d' file.txt > new_file.txt Alternatively, use the in‑place edit flag:

sed -i '1 d' file.txt

3. How to check the length of a specific line in a text file?

Combine sed with wc -c to count characters. For example, to get the length of line 5 in linuxmi.txt:

sed -n '5 p' linuxmi.txt | wc -c

4. How to display all non‑printable characters in a file?

Open the file with vi, enter command mode, and run: set list This shows characters such as ^M (Ctrl+M) and other invisible symbols.

5. How to create a shared directory where members can create files but only the owners can delete them?

Use a sticky bit on the directory:

mkdir dir_xyz
chmod g+wx dir_xyz
chmod +t dir_xyz

The sticky bit ( +t) ensures that only the file’s owner, the directory’s owner, or root can delete files within the directory.

6. What are the typical lifecycle stages of a Linux process?

Waiting – the process is idle, waiting for resources.

Running – the process is actively executing.

Stopped – the process has been halted, either after completion or by a signal.

Zombie – the process has terminated but still occupies an entry in the process table.

7. How to use the cut command?

Extract the first 10 characters of a file: cut -c1-10 txt_linuxmi Extract specific columns (e.g., 2, 5, and 7) using a delimiter:

cut -d';' -f2 -f5 -f7 txt_linuxmi

8. What is the difference between cmp and diff ?

diff

shows the changes needed to make two files identical, presenting a line‑by‑line comparison. cmp performs a byte‑by‑byte comparison and reports the first mismatching byte.

9. Can the ls command be replaced by echo ?

Yes. echo * expands to the same list of filenames that ls would display, though formatting differs.

10. What is an inode?

An inode is a data structure used by Linux/Unix file systems to store metadata about a file, including a unique inode number that identifies the file on the disk.

These ten Q&A pairs cover essential Linux shell knowledge often asked in technical interviews.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxinterviewcommand-lineUnix
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.