Fundamentals 7 min read

Top 10 Linux Shell Interview Questions and How to Solve Them

This article presents ten common Linux shell interview questions covering script termination, file header removal, line length checking, viewing non‑printing characters, directory permissions, process lifecycle, cut usage, cmp vs diff differences, echo vs ls substitution, and a brief explanation of inodes, each with clear command‑line solutions.

Efficient Ops
Efficient Ops
Efficient Ops
Top 10 Linux Shell Interview Questions and How to Solve Them

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

Use the exit command. Supplying a non‑zero status, e.g. exit -1, forces the script to terminate with an error.

#!/bin/bash
 echo "Hello"
 exit -1
 echo "bye"

Running the script shows the exit -1 stops execution before the final echo runs.

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

The sed command can delete the first line:

# sed '1 d' file.txt

Using the in‑place edit flag -i avoids redirection:

# sed -i '1 d' file.txt

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

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

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

4. How to view all non‑printing characters in a file?

Open the file in vi and enable the list option:

vi file.txt
: set list

This displays characters such as ^M for carriage returns.

5. How to create a shared directory dir_xyz where group members can create or access files but only the creator can delete them?

Use the sticky bit together with group write/execute permissions:

# mkdir dir_xyz
# chmod g+wx dir_xyz
# chmod +t dir_xyz

The +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 lifecycle?

Waiting – the process is waiting for resources.

Running – the process is actively executing.

Stopped – the process has finished execution or received a stop signal.

Zombie – the process has terminated but remains 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 fields (e.g., columns 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, while cmp compares files byte‑by‑byte and reports the first mismatch.

9. Can the echo command replace ls ?

Yes. echo * lists the contents of the current directory, producing the same output as ls in many cases.

10. What is an inode?

An inode is a data structure used by Linux/Unix file systems to store metadata about a file; each file has a unique inode number.

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.

linuxinterviewShellcommand-lineUnix
Efficient Ops
Written by

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.

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.