Software Training Institute in Chennai with 100% Placements – SLA Institute
Share on your Social Media

Unix Shell Scripting Interview Questions and Answers

Published On: February 15, 2025

Introduction

Preparing for interviews requires you to know basic concepts like Unix shell scripting. Unix shell scripting helps in automating tasks. It also helps in managing system operations. This improves efficiency in Unix and Linux environments. Shell scripting is still important for IT jobs. This guide on Unix Shell Scripting Interview Questions and Answers will help both people and those with experience. It explains questions in a simple way. The guide is structured to make it easy to understand Unix shell scripting concepts. This will help you do well in interviews. Start your journey with our Unix Shell Scripting Course Syllabus designed for beginners and professionals.

Unix Shell Scripting Interview Questions for Freshers

1. What is a Shell and what is Shell Scripting?

  • A Shell is a command-line tool that acts as a bridge between the user and the Unix/Linux operating system. It allows users to interact with the system using commands.
  • Shell scripting is the process of writing multiple commands in a file and executing them together. It helps automate repetitive tasks and saves time in system operations.

2. What is the function of the shebang (#!) line in a script?

  • The shebang (#!) is written at the top of a script.
  • It indicates which interpreter the system should use.
  • Common example: #!/bin/bash.
  • Without it, the script may not run correctly.

3. How do you make a script executable and run it?

To run a shell script, follow these steps:

  • Give execute permission:
    • chmod +x script_name.sh
  • Run the script using:
    • ./script_name.sh

4. What are the different types of variables used in Shell scripts?

  • System-defined variables
    • Created by the operating system.
    • Usually written in uppercase.
    • Examples: $PATH, $HOME, $SHELL.
  • User-defined variables
    • Created by users.
    • Used to store custom data.
    • Example: name=”John”.

5. Explain positional parameters in Shell scripting.

Positional parameters are values passed to a script when it runs. They help in handling user input dynamically.

  • $0 → Script name
  • $1, $2, … → Arguments passed.
  • $# → Total number of arguments.

6. What is the difference between $* and $@?

Both are used to access all command-line arguments, but they behave differently:

  • “$*” → It handles all arguments as one continuous string.
  • “$@” → Treats each argument separately.

7. How do you check if the previous command was successful?

  • Use the special variable $?.
  • It stores the result of the last executed command.
  • 0 → Success.
  • Non-zero value → Error or failure.

8. What are the common types of loops available?

Unix shell scripting supports different loops to repeat tasks:

  • for loop → Runs for a fixed list of values.
  • while loop → It continues to run while the condition is true.
  • until loop → Runs until a condition becomes true.

9. How do you read user input in a script?

The read command is used to take input from the user during execution.

Example:

read -p “Enter your name: ” username

This stores the input in a variable.

10. What distinguishes break from continue?

  • break
    • Stops the loop completely.
  • continue
    • Skips the current step.
    • Moves to the next iteration.

11. What are the ways to debug a shell script?

Debugging helps identify errors in your script. You can enable debug mode in two ways:

  • Add set -x inside the script.
  • Run using: bash -x script.sh.

This shows each command before execution, making it easier to find issues.

12. What is a “Here Document” (Heredoc)?

  • A here document allows you to send multi-line input to a command inside a script.
  • Useful for passing in large amounts of text or when carrying out automation tasks.

13. What is the difference between a Hard Link and a Soft (Symbolic) Link?

  • Hard Link
    • Shares the same inode as the original file.
    • Works even if the original file is deleted.
  • Soft Link (Symbolic Link)
    • Acts like a shortcut.
    • Breaks if the original file is removed.

14. What are “crontab” and “cron jobs”?

  • Crontab is a file used to schedule tasks.
  • Cron jobs are automated tasks that run at specific times.
  • Commonly used for backups, updates, and maintenance.

15. What are sed and awk used for?

  • sed (Stream Editor)
    • Used for text editing.
    • Mainly for search and replace.
  • awk
    • Used for data processing.
    • Helps in generating reports from text files.

Learn with simple, step-by-step Unix Shell Scripting tutorials designed for beginners.

Unix Shell Scripting Interview Questions for Experienced Candidates

1. How do you handle errors and ensure a script stops on failure?

Error handling is essential for writing reliable scripts.

  • set -e makes the script stop execution when a command fails.
  • The trap command can be used to catch errors or signals.
  • Helps perform cleanup tasks before the script exits.

2. How do you debug a complex shell script?

Debugging makes it easier to detect problems in complex scripts.

  • set -x → Displays each command before execution.
  • set -v → Prints each line as it is read.

Tools like ShellCheck help detect syntax and logic errors.

3. Explain the significance of [[ ]] vs [ ].

  • [[ ]] is an advanced and safer test command.
  • It supports pattern matching along with logical operators like && and ||.
  • Reduces the need for quoting variables.
  • [ ] is the traditional test command.
  • Less flexible compared to [[ ]].

4. How do you perform arithmetic operations in Bash?

Arithmetic operations can be handled using built-in methods:

  • (( )) → Preferred for integer calculations.
  • let → Another option for arithmetic.

For decimal calculations:

  • Use external tools like bc.

5. What is a “Zombie Process” and how do you handle it in a script?

A zombie process is a completed process that still exists in the process table.

  • Occurs when the parent process does not read the exit status.
  • The wait command helps clean up such processes.
  • Prevents unnecessary resource usage.

6. How do you monitor a log file in real-time?

Real-time monitoring is useful for tracking system activity.

  • tail -f file.log → Continuously displays new entries.
  • Combine with grep to filter specific patterns.
    • Example: tail -f file.log | grep “error”.

7. What is IFS, and how is it used?

IFS is a shell variable that defines how input is split into words.

  • Default: space, tab, newline.
  • Can be changed to handle custom separators (like commas).
  • Commonly used with read and string parsing.

8. How do you pass and handle multiple options/flags in a script?

  • Use the getopts command.
  • Helps process command-line options efficiently.
  • Supports structured and professional script design.

9. How do you run a script in the background and keep it running after logout?

  • Use: nohup ./script.sh &.
  • & runs the script in the background.
  • nohup prevents termination after logout.

10. How do you compare two strings in a shell script?

String comparison is done using conditional expressions:

  • Use either == or = when working within [[ ]] or [ ].
  • Example: [[ “$str1” == “$str2” ]].

11. How do you define and use arrays in Bash?

An array stores multiple values in one variable.

  • Declaration: array_name=(val1 val2 val3).
  • Access element: ${array_name[index]}.
  • Access all values: ${array_name[@]}.

12. How do you create and use associative arrays?

Associative arrays store data using keys instead of numbers.

  • Declare using: declare -A my_array.
  • Assign values: my_array[key]=”value”.
  • Supported in Bash 4 and above.

13. How can you extract a substring without calling external commands?

Bash provides built-in parameter expansion for this purpose.

  • Syntax: ${VAR:position:length}.
  • Faster than using tools like cut or awk.

14. What is the significance of the exec command within a script?

  • exec runs a command by replacing the existing shell process.
  • No new process is created.
  • Useful for performance optimization and process control.

15. How do you run a command and capture its PID?

  • Run the command in the background using &.
  • Use $! to get the Process ID (PID).
  • Helps in monitoring or terminating the process later.

Join our Unix Shell Scripting Course in Chennai and upgrade your technical skills.

Conclusion

In conclusion, Unix shell scripting is still really important for automating tasks, managing system operations, and boosting productivity in real-world environments. This guide on Unix Shell Scripting Interview Questions and Answers helps you understand both basic and advanced Unix shell scripting concepts in a way. You can master Unix shell scripting with practice. When you have a grasp of Unix shell scripting topics, handling scripting challenges and doing well in technical interviews gets easier. Unix shell scripting makes it all possible. Get professional career support and expert guidance from our leading Training and Placement Institute in Chennai to help you achieve your job goals.

Share on your Social Media

Just a minute!

If you have any questions that you did not find answers for, our counsellors are here to answer them. You can get all your queries answered before deciding to join SLA and move your career forward.

We are excited to get started with you

Give us your information and we will arange for a free call (at your convenience) with one of our counsellors. You can get all your queries answered before deciding to join SLA and move your career forward.