0% found this document useful (0 votes)
28 views7 pages

25- Linux Shell Interview Questions for Intermediate Level with Answers

Uploaded by

abhikumbhar1908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views7 pages

25- Linux Shell Interview Questions for Intermediate Level with Answers

Uploaded by

abhikumbhar1908
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

25+ Linux Shell Interview Questions for Intermediate Level with Answers

1. What are the different types of shells available in Linux? How do you switch between
them?

Answer:

​ Types of Shells: Bash, Zsh, Ksh, Tcsh, Fish, etc.


​ Switching: Use the shell's name to switch:

zsh # Switch to Zsh

2. How do you pass arguments to a shell script?

Answer:
Arguments are passed via the command line and accessed using $1, $2, etc.
Example:

./script.sh arg1 arg2

echo $1 # Prints arg1

3. Explain the difference between source and ./ when running a script.

Answer:

​ source: Runs the script in the current shell session. Changes affect the parent shell.
​ ./: Executes the script in a new subshell, isolating changes.

4. How do you redirect both stdout and stderr to the same file?

Answer:
Use 2>&1 or &>:

command > output.log 2>&1


5. What is a Here Document (Heredoc) in Linux?

Answer:
A Heredoc redirects a block of text into a command:

cat << EOF

This is a heredoc example.

EOF

6. How do you compare strings in a shell script?

Answer:
Use conditional statements:

if [ "$string1" = "$string2" ]; then

echo "Strings are equal"

fi

7. How do you create and use an alias persistently?

Answer:

​ Temporary Alias:

alias ll="ls -la"

​ Persistent Alias: Add to ~/.bashrc or ~/.bash_profile:

echo 'alias ll="ls -la"' >> ~/.bashrc


8. How do you find the size of a directory?

Answer:
Use the du command:

du -sh /path/to/directory

9. What is the difference between kill and killall commands?

Answer:

​ kill: Terminates a process using its PID.


​ killall: Terminates all processes by name.

10. How do you write a simple for loop in a shell script?

Answer:

for i in {1..5}; do

echo "Iteration $i"

done

11. What is the significance of set -e in shell scripts?

Answer:
set -e causes the script to exit immediately if a command fails.

12. How do you check if a file exists in a shell script?

Answer:
Use the -e option:
if [ -e file.txt ]; then

echo "File exists"

fi

13. Explain the difference between crontab and at commands.

Answer:

​ crontab: Schedules recurring tasks.


​ at: Schedules a one-time task.

14. How do you find all files larger than 10MB in a directory?

Answer:
Use the find command:

find /path -size +10M

15. What is the difference between $* and "$@" in a shell script?

Answer:

​ $*: Treats all arguments as a single word.


​ "$@": Preserves argument boundaries as separate words.

16. How do you schedule a cron job to run every 15 minutes?

Answer:
Add this to crontab:

*/15 * * * * /path/to/script.sh
17. How do you create a function in a shell script?

Answer:

my_function() {

echo "This is a function"

my_function

18. How do you extract a specific column from a file?

Answer:
Use the awk command:

awk '{print $2}' file.txt # Extracts the 2nd column

19. How do you find the number of lines in a file?

Answer:
Use the wc -l command:

wc -l file.txt

20. How do you append output to an existing file?

Answer:
Use the >> operator:

echo "New content" >> file.txt


21. What is the difference between exec and fork?

Answer:

​ exec: Replaces the current process with a new one.


​ fork: Creates a new process.

22. How do you debug a shell script?

Answer:
Use set -x to enable debugging:

set -x

23. How do you handle signals in a shell script?

Answer:
Use the trap command:

trap "echo Signal received" SIGINT

24. How do you compress multiple files into a .tar.gz file?

Answer:

tar -czvf archive.tar.gz file1 file2

25. How do you extract only specific files from a .tar archive?

Answer:

tar -xvf archive.tar file1 file2

You might also like