Fundamentals 7 min read

Top Linux Shell Interview Questions and Practical Answers

This article presents ten common Linux shell interview questions, each followed by concise explanations and ready‑to‑run command‑line examples that illustrate how to abort scripts, manipulate files, inspect characters, manage directories, and understand process states.

Open Source Linux
Open Source Linux
Open Source Linux
Top Linux Shell Interview Questions and Practical Answers

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

Answer: Use the exit command. Exiting with a non‑zero status (e.g., exit -1) causes the script to terminate with an error.

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

Running the script shows the output before the exit -1 command stops execution.

2. How to remove the header line from a file?

Answer: The sed command can delete the first line of a file. # sed '1 d' file.txt Using the in‑place option -i achieves the same result without redirection:

# sed -i '1 d' file.txt

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

Answer: Combine sed to select the line with wc -c to count characters.

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

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

Answer: Open the file in vi and enable the list option.

vi file.txt
: set list
Note: This also reveals characters such as ^M (carriage return).

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

Answer: Use the sticky bit on the directory.

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

The +t (sticky) permission ensures only the file owner, directory owner, or root can delete files.

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

Waiting – the process is waiting for resources.

Running – the process is actively executing.

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

Zombie – the process has terminated but remains in the process table.

7. How to use the cut command?

Answer: cut extracts selected columns or characters from a file. # cut -c1-10 txt_linuxmi To extract the 2nd, 5th, and 7th fields separated by semicolons:

# cut -d';' -f2,5,7 txt_linuxmi

8. What is the difference between cmp and diff ?

cmp

compares two files byte by byte and reports the first mismatch. diff shows the changes needed to make the files identical, providing a line‑by‑line difference.

9. Can echo replace ls ?

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

10. What is an inode?

Answer: An inode is a data structure used by Linux to represent a file; each file has a unique inode number that stores metadata about the file.

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-lineScripting
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.