Fundamentals 8 min read

Top 10 Linux Shell Interview Questions with Clear Answers

This article presents ten common Linux shell interview questions, explains how to interrupt scripts, manipulate files with sed, check line lengths, view non‑printable characters, set directory permissions, understand process states, and use commands like cut, cmp, diff, echo, and inode.

ITPUB
ITPUB
ITPUB
Top 10 Linux Shell Interview Questions with Clear Answers

This guide answers ten frequently asked Linux shell interview questions, providing concrete command examples and explanations.

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

Use the exit command with a non‑zero status. For example, placing exit -1 in the script will cause it to terminate with an error.

#!/bin/bash

echo "Hello"
exit -1

echo "bye"

Running the script shows the error message and the subsequent echo "bye" still executes because the typo exit-1 is not recognized.

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

The sed command can delete the first line. To save the result, redirect the output or use the -i in‑place option.

# sed '1d' file.txt > new_file.txt
# sed -i '1d' file.txt

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

Combine sed to print the line and pipe to wc -c to count characters.

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

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

Open the file in vi, enter command mode, and execute set list. This reveals characters such as ^M.

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

Use the sticky bit and group write/execute permissions:

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

The sticky bit ( +t) 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 been halted or received a kill signal.

Zombie : The process has terminated but remains in the process table.

7. How to use the cut command?

cut

extracts specific columns or character ranges from a file.

# cut -c1-10 txt_linuxmi          # first 10 characters
# cut -d';' -f2,5,7 txt_linuxmi   # fields 2, 5, and 7 using ';' as delimiter

8. 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 echo replace ls ?

Yes, echo * lists directory contents similarly to ls, though formatting differs.

10. What is an inode?

An inode is a data structure on a Unix/Linux filesystem that stores metadata about a file, including 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.

interviewShellScriptingcommands
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

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.