Shell Initialization and Termination

In order to support user customization, shells read certain specified files on startup, and for some shells, also on termination. Each shell has different conventions, so we discuss them in separate sections.

If you write shell scripts that are intended to be used by others, you cannot rely on startup customizations. All of the shell scripts that we develop in this book set up their own environment (e.g., the value of $PATH) so that anyone can run them.

Shell behavior depends on whether it is a login shell. When you sit at a terminal and enter a username and password in response to a prompt from the computer, you get a login shell. Similarly, when you use ssh hostname, you get a login shell. However, if you run a shell by name, or implicitly as the command interpreter named in the initial #! line in a script, or create a new workstation terminal window, or run a command in a remote shell with—for example, ssh hostname command—then that shell is not a login shell.

The shell determines whether it is a login shell by examining the value of $0. If the value begins with a hyphen, then the shell is a login shell; otherwise, it is not. You can tell whether you have a login shell by this simple experiment:

$ echo $0                                
            Display shell name
-ksh                                     Yes, this is a login shell

The hyphen does not imply that there is a file named /bin/-ksh. It just means that the parent process set the zeroth argument that way when it ran the exec( ) system call to start the shell. ...

Get Classic Shell Scripting 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.