30+ Shell Scripting Interview Questions

Mounish V Last Updated : 19 Jul, 2024
4 min read

Introduction

Shell scripting is a fundamental skill that helps system administrators and developers to streamline tasks. It helps them effectively manage systems and handle files efficiently within a command-line environment. This skill is vital for technical interviews in the IT and development fields. So, here are some shell scripting interview questions, that will prepare you well for your interviews and enhance your proficiency.

Shell Scripting Interview Questions

Here are the 31 most commonly asked interview questions on shell scripting.

Q1. What is a shell script?

Ans. A shell script is a script written for a shell, or command-line interpreter, typically for automating administrative tasks and system management.

Q2. How does shell scripting differ from programming?

Ans. Shell scripting automates administrative tasks and system processes, while programming focuses on creating applications with complex logic.

Q3. Explain the difference between single and double square brackets in shell scripting.

Ans. Single square brackets `[ ]` are a POSIX compliant test command, while double square brackets `[[ ]]` are an enhanced version with additional features such as pattern matching and string manipulation.

Q4. How do you comment out multiple lines of code in a shell script?

Ans. Multiple lines can be commented out by enclosing them between `: ‘` and `’`, or `<<COMMENT … COMMENT`.

Q5. What is the purpose of the `chmod` command in shell scripting?

Ans. The `chmod` command changes the permissions of a file or directory, allowing or denying specific users’ read, write, and execute permissions.

Q6. How can you pass arguments to a shell script?

Ans. Arguments can be passed to a shell script by listing them after the script name when executing it, for example, `./script.sh arg1 arg2`.

Q7. Can you describe the purpose of the shebang line?

Ans. The shebang line, found at the script’s beginning, identifies the script interpreter’s path and is used to run the script.

Q8. What are some standard streams in Linux?

Ans. Linux provides three standard streams: Standard Input, Standard Output, and Standard Error.

Q9. What alternative command can replace echo?

Ans. The command tput can be used instead of echo, giving control over output display formatting on the screen.

Q10. What do you know about GUI scripting?

Ans. GUI scripting involves using a graphical user interface to manage computer operations and applications, relying on the operating system for support.

Shell scripting interview questions

Q11. How does a shell variable function?

Ans. Shell variables are fundamental in shell scripts, enabling the storage and manipulation of data, typically stored as string variables.

Q12. What are the disadvantages associated with shell scripting?

Ans. Shell scripting has several drawbacks: it starts slowly but speeds up, is not easily portable, can cause data loss due to typing errors, and poor designs can be costly and disruptive.

Q13. What does the Crontab command do?

Ans. Crontab, or cron table, refers to a list of scheduled commands managed by the cron job scheduler, used to execute tasks at regular intervals.

Q14. What stages does a Linux process go through?

Ans. A Linux process has four stages: Waiting (for resources), Running (currently executing), Stopped (post-execution), and Zombie (active in process table but stopped).

Q15. Explain the role of metacharacters in a shell?

Ans. Metacharacters are special characters in a shell that provide information about other characters. For instance, using ls s* lists all files starting with ‘s’.

Q16. How can you connect to a database server in Linux?

Ans. Use the isql utility, included with the open client driver, to connect to a database server:

isql –S serverName –U username –P password

Q17. What is the purpose of a superblock in shell scripting?

Ans. A superblock contains vital records of a specific file system, such as size, counts of filled and empty blocks, usage information, block groups’ size, and inode tables’ location and size.

Q18. What do you understand by a shell in computing?

Ans. A shell is a command-line interpreter that converts user-entered commands into a form understandable by the kernel, executing the required programs.

Q19. What is the significance of the ‘$#’ variable in shell scripting?

Ans. The ‘$#’ variable displays the total number of arguments passed to a script.

Q20. Differentiate between $@ and $* in shell scripting.

Ans. $* treats all positional arguments as a single string, whereas $@ treats each quoted argument separately.

Q21. What common shells are used in a Linux environment?

Ans. Common Linux shells include the C-Shell (e.g., TENEX C-Shell, Z-Shell) and Bourne Shell (e.g., Korn Shell, POSIX Shell, Bourne-Again Shell).

Q22. Can shell scripts be executed on different Unix-like systems?

Ans. Yes, though there may be differences in shell syntax and commands across systems.

Q23. How many kernels and shells exist in a UNIX environment?

Ans. In UNIX, multiple shells can exist, but there is only a single kernel.

Q24. How do you append output to a file in a shell script?

Ans. Use the >> operator to append output to a file. Example: echo "text" >> file.txt.

Q25. What are special variables in shell scripting?

Ans. Special variables include $0 (script name), $# (number of arguments), $* (all arguments), and $@ (all arguments as separate strings).

Q26. How do you debug a shell script?

Ans. Enable debugging with the -x option, which prints each command before execution. Example: bash -x scriptname.sh.

Q27. What is the purpose of awk in shell scripting?

Ans. awk is a powerful text processing and data extraction language used within scripts.

Q28. How can you find and replace text in a file using shell scripting?

Ans. Use the sed command to find and replace text. Example: sed 's/oldtext/newtext/g' file.txt.

Q29. What does the eval command do in shell scripting?

Ans. The eval command executes arguments as a shell command.

Q30. What are some common use cases for shell scripting?

Ans. Automating backups, system monitoring, batch file processing, and user account management.

Q31. What are the best practices for writing shell scripts?

Ans. Use meaningful variable names, add comments for clarity, handle errors, and avoid hard-coding values.

Conclusion

One skill that greatly increases the productivity of both developers and system administrators is shell scripting. It guarantees efficient system administration, streamlines processes, and manipulates files with skill via command-line interfaces. Technical interviews in the IT and development domains require proficiency in this area. Knowing these 31 crucial shell scripting interview questions will help you prepare, increasing your preparedness and shell scripting proficiency.

I'm a tech enthusiast, graduated from Vellore Institute of Technology. I'm working as a Data Science Trainee right now. I am very much interested in Deep Learning and Generative AI.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details