Shell and Command Questions

This section covers some of the common questions about the shell itself and how the shell executes commands.

Why does #!/bin/sh have to be the first line of my scripts?

Chapter 2, “Script Basics,” stated that #!/bin/sh must be the first line in your script to ensure that the correct shell is used to execute your script. This line must be the first line in your shell script because of the underlying mechanism used by a shell to execute commands.

When you ask a shell to execute a command as follows

$ date

The shell uses the exec system call to ask the UNIX kernel to execute the command you requested. System calls are C language functions built in to the UNIX kernel that enable you to access features of the kernel

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.