Most Frequently asked shell Interview Questions (2024)

author image Hirely
at 02 Jan, 2025

Question: What is the difference between $? and $$ in Shell?

Answer:

In Shell scripting, $? and $$ are special variables that provide information about the state of the script or the process. Here’s the difference between them:

1. $?: Exit Status of the Last Command

  • Purpose: The $? variable holds the exit status (also known as the return value or exit code) of the last executed command.
  • Range: The exit status is an integer value where 0 typically means the command executed successfully, and any non-zero value indicates an error or failure.

Example:

ls /nonexistent_directory
echo $?
  • The ls command will fail because the directory doesn’t exist, and $? will return a non-zero exit status (usually 2 for ls in this case).
  • The echo $? will display 2, which is the exit status of the last command (ls).

Common Usage:

  • If you want to check if a command was successful and take action based on that, you can use $?.
if [ $? -eq 0 ]; then
    echo "Command succeeded"
else
    echo "Command failed"
fi

2. $$: Process ID of the Current Shell

  • Purpose: The $$ variable holds the process ID (PID) of the current running shell or script.
  • Usage: It’s often used to generate unique temporary file names or identify the running process.

Example:

echo $$
  • If you run this command in a shell, it will return the PID of that shell session. For example, it might return 12345, which is the PID of the shell process.

Common Usage:

  • Generating unique filenames using $$ to avoid conflicts:
    temp_file="tempfile_$$.txt"
    echo "This is a temporary file" > $temp_file

Key Differences:

VariablePurposeValueCommon Use
$?Exit status of the last commandExit code (0 for success, non-zero for error)Check the success or failure of the last command
$$Process ID of the current shell/commandProcess ID (PID) of the shell or scriptGenerate unique temporary filenames or track the shell’s PID

Summary:

  • $? provides the exit status of the last executed command, helping you determine whether it was successful or not.
  • $$ provides the PID of the current shell or script, useful for tracking processes or creating unique identifiers.

Question: How can you redirect both stdout and stderr to a file in Shell?

Answer:

In Shell scripting, you can redirect both standard output (stdout) and standard error (stderr) to a file by using file descriptors and redirection operators. Here’s how you can do that:

1. Using > for Redirecting stdout and 2> for stderr:

You can redirect both the standard output and the standard error to the same file using the following method:

command > outputfile 2>&1

Explanation:

  • >: Redirects the standard output (stdout) of the command to a file.
  • 2>: Redirects the standard error (stderr) to a file.
  • 2>&1: This tells the shell to redirect file descriptor 2 (stderr) to the same place as file descriptor 1 (stdout). In this case, it combines both stdout and stderr and writes them to outputfile.

Example:

ls /nonexistent_directory > output.txt 2>&1
  • This command attempts to list the contents of a nonexistent directory. The standard output and the error message (which is written to stderr) will both be redirected to the file output.txt.

2. Using &> for Simpler Redirection (Bash-specific):

If you are using Bash, you can use the &> operator, which is a shorthand to redirect both stdout and stderr to the same file.

command &> outputfile

Example:

ls /nonexistent_directory &> output.txt
  • This command works the same way as the previous one, redirecting both stdout and stderr to output.txt.

3. Appending to a File:

If you want to append both stdout and stderr to an existing file instead of overwriting it, you can use >> for stdout and 2>> for stderr:

command >> outputfile 2>&1

or using the &>> shorthand in Bash:

command &>> outputfile

Example:

ls /nonexistent_directory >> output.txt 2>&1
  • This command appends both the standard output and standard error to output.txt.

Summary of Methods:

  1. Redirect both stdout and stderr to a file:

    • command > outputfile 2>&1
    • command &> outputfile (Bash-specific)
  2. Append both stdout and stderr to a file:

    • command >> outputfile 2>&1
    • command &>> outputfile (Bash-specific)

These methods ensure that both the normal output and any error messages from the command are saved in the same file, making it easier to capture both types of output for troubleshooting or logging purposes.

Question: What is the purpose of the awk command?

Answer:

The awk command is a powerful text-processing utility in Unix-like operating systems, used primarily for pattern scanning, data extraction, and reporting. It is a pattern-matching and processing language that is especially useful for working with structured text files (such as CSV, tab-delimited files, or logs) where data is organized in columns.

Key Purposes of awk:

  1. Text Processing and Transformation:

    • awk can be used to process and transform text based on specified patterns. You can extract, modify, and format data from files or input streams.
  2. Field-Based Processing:

    • awk processes text by breaking it into fields (columns) based on delimiters (like spaces, tabs, commas). It makes it easy to perform operations on specific columns of data.
  3. Pattern Matching:

    • awk allows you to match specific patterns in input lines, and perform actions based on whether those patterns are found.
  4. Data Extraction:

    • awk can extract specific columns or fields from files or input and display them, often used for summarizing and analyzing data.
  5. Report Generation:

    • It can be used to generate reports, perform calculations on the data, and format the output into a human-readable form.

Syntax:

awk 'pattern { action }' input_file
  • pattern: The condition to match (e.g., a regular expression, a specific line number, etc.).
  • action: The operation or task to perform when the pattern is matched (e.g., printing a column, summing values).
  • input_file: The file(s) to process. If no file is provided, awk reads from standard input.

Common Uses of awk:

  1. Print Specific Columns (Fields):

    • In awk, $1 refers to the first field (column), $2 refers to the second, and so on. $0 refers to the entire line.

    Example:

    awk '{ print $1, $3 }' input.txt
    • This command will print the first and third columns of each line in input.txt.
  2. Using Field Separators:

    • By default, awk uses spaces or tabs to separate fields. You can specify a custom field separator using the -F option.

    Example:

    awk -F "," '{ print $1, $3 }' input.csv
    • This will treat commas as field separators and print the first and third columns of a CSV file.
  3. Pattern Matching:

    • You can specify a pattern to match specific lines in the input.

    Example:

    awk '/error/ { print $0 }' log.txt
    • This will print all lines from log.txt that contain the word “error”.
  4. Performing Calculations:

    • awk can be used to perform mathematical calculations on columns of data.

    Example:

    awk '{ sum += $2 } END { print sum }' input.txt
    • This sums all the values in the second column of input.txt and prints the total.
  5. Conditionally Perform Actions:

    • You can specify conditions to control when certain actions are performed.

    Example:

    awk '$3 > 50 { print $1, $2 }' input.txt
    • This prints the first and second columns of lines where the value in the third column is greater than 50.
  6. Using Built-In Variables:

    • awk has several built-in variables like NR (number of records/lines), NF (number of fields in a line), and FS (input field separator).

    Example:

    awk '{ print NR, $0 }' input.txt
    • This prints the line number (NR) followed by the entire line ($0) for each line in input.txt.

Example Commands:

  • Print the first column of a file:

    awk '{ print $1 }' file.txt
  • Print lines where the second column is greater than 10:

    awk '$2 > 10 { print $0 }' file.txt
  • Sum values in the second column and print the total:

    awk '{ sum += $2 } END { print sum }' file.txt
  • Change the field separator to a comma and print specific columns:

    awk -F "," '{ print $1, $2 }' file.csv

Summary:

  • awk is a powerful tool for text processing and data extraction in Unix-like systems. It allows you to process text by fields, match patterns, and perform calculations or transformations.
  • Common tasks include printing specific columns, performing arithmetic on data, matching patterns, and generating formatted reports.

It is widely used for parsing logs, CSV files, and performing quick data manipulations directly in the shell.

Question: Explain how the for loop works in Shell scripting.

Answer:

In Shell scripting, the for loop is used to repeat a set of commands multiple times. It allows you to iterate over a list of values or a range of numbers, executing commands for each value in the list or range.

There are several variations of the for loop in Shell scripting, each suited for different purposes. Here’s an explanation of the most common forms.

1. Basic for loop with a list of values:

In this form, the for loop iterates over a list of values, executing the commands for each value in the list.

Syntax:

for var in value1 value2 value3 ...
do
    command1
    command2
    ...
done
  • var: A variable that will take on each value from the list one by one.
  • value1, value2, ...: The list of values that var will iterate through.
  • do ... done: The block of code that is executed for each value in the list.

Example:

for color in red green blue
do
    echo "Color is $color"
done
  • This will output:
    Color is red
    Color is green
    Color is blue

2. for loop with a range of numbers (C-style syntax):

In this form, the for loop uses a C-style syntax, where you specify a starting value, an ending value, and an increment step. It’s useful when you need to repeat a command a certain number of times or iterate over a sequence of numbers.

Syntax:

for (( i=start_value; i<=end_value; i+=increment ))
do
    command1
    command2
    ...
done
  • i=start_value: The initial value of the loop variable.
  • i<=end_value: The condition that keeps the loop running until i reaches end_value.
  • i+=increment: The step by which i will change after each iteration.

Example:

for (( i=1; i<=5; i++ ))
do
    echo "Iteration number $i"
done
  • This will output:
    Iteration number 1
    Iteration number 2
    Iteration number 3
    Iteration number 4
    Iteration number 5

In this example, the loop starts with i=1 and increments i by 1 each time, repeating the loop until i reaches 5.

3. for loop with a list generated by a command or expression:

You can also use a command or an expression to generate a list of values for the loop to iterate over.

Syntax:

for var in $(command)
do
    command1
    command2
    ...
done
  • $(command): Executes the command and uses its output as the list of values for the loop.

Example:

for file in $(ls)
do
    echo "File: $file"
done
  • This will iterate over each file in the current directory and print its name.

4. for loop with a sequence of numbers (using seq command):

You can also use the seq command to generate a sequence of numbers, and the for loop will iterate over that sequence.

Syntax:

for i in $(seq start_value end_value)
do
    command1
    command2
    ...
done

Example:

for i in $(seq 1 5)
do
    echo "Number: $i"
done
  • This will output:
    Number: 1
    Number: 2
    Number: 3
    Number: 4
    Number: 5

5. Using break and continue inside the for loop:

  • break: Exits the loop immediately, skipping any remaining iterations.
  • continue: Skips the current iteration and moves to the next one.

Example of break:

for i in 1 2 3 4 5
do
    if [ $i -eq 3 ]; then
        break
    fi
    echo "Number: $i"
done
  • This will output:
    Number: 1
    Number: 2
    The loop breaks when i equals 3, and no further iterations are executed.

Example of continue:

for i in 1 2 3 4 5
do
    if [ $i -eq 3 ]; then
        continue
    fi
    echo "Number: $i"
done
  • This will output:
    Number: 1
    Number: 2
    Number: 4
    Number: 5
    The loop skips the iteration where i equals 3, and continues with the next iteration.

Summary of for loop variations:

SyntaxUse Case
for var in value1 value2 value3 ...Iterate over a predefined list of values.
for (( i=start_value; i<=end_value; i+=increment ))Iterate over a range of numbers with a specified step.
for var in $(command)Iterate over the output of a command.
for i in $(seq start_value end_value)Iterate over a sequence of numbers generated by seq.
breakExit the loop prematurely.
continueSkip the current iteration and continue with the next.

Example of Using for Loop to Process Files:

for file in *.txt
do
    echo "Processing $file"
    # Additional processing commands
done
  • This for loop processes all .txt files in the current directory.

Conclusion:

The for loop in Shell scripting is versatile and can be used to iterate over lists, ranges, and sequences, providing a powerful way to automate repetitive tasks in scripts. It can also be customized with conditions like break and continue to control the flow of execution.

Read More

If you can’t get enough from this article, Aihirely has plenty more related information, such as shell interview questions, shell interview experiences, and details about various shell job positions. Click here to check it out.

Trace Job opportunities

Hirely, your exclusive interview companion, empowers your competence and facilitates your interviews.

Get Started Now