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 shellThe 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. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access