When logging into a UNIX/Linux box, which profile scripts are executed?

Typically there are several different profile scripts that set the $PATH variable and other aspects of the user's environment. However in most user's I've seen, they have .cshrc for CSH, .bashrc for bash, kshrc for ksh, etc..

Do they all run upon login? Is it based on the user's default shell? How does the login script mechanism work?

For example when using su - which script would run?

1 Answer 1


That depends completely on the shell that is being started.

The man page of each shell has a chapter describing which script(s) that shell executes on login or logout, and in which order. The chapter might be titled Startup and shutdown (man tcsh) or INVOCATION (man bash, man zsh).

Which script(s) get executed can vary based on whether the shell is a login shell or not, and/or whether the shell is started in interactive mode or not.

This allows avoiding unnecessary duplication of effort: for example, a shell that's executed via another program's "escape to shell" feature does not need to re-do environment variable settings or any other inheritable settings, because it presumably already inherited them from its parent process, which ultimately got them from the login shell.

Likewise, a non-interactive shell can (and should!) skip any initialization procedures that would produce output, as the output of the shell (and the command line it's been supplied with at startup) might be going to be processed by some other program. In that situation, "human-friendly" outputs are useless and might even be harmful to the intended processing.

In the specific case of running su -, the su command will thoroughly reinitialize the environment to match the state of a text-mode login as root, and then will execute the default shell for the root user, in "login shell mode", just like it was a real login. Since no -c option was used, the shell will be interactive. In almost all cases, this shell will be a POSIX-compatible /bin/sh or equivalent, to maximize start-up script compatibility for third-party software packages.

(Changing the root's shell away from /bin/sh or whatever is the factory default for the OS is usually a bad idea. Some early start-up scripts may depend on the shell working exactly as expected.)

When started as a login shell, a POSIX sh-compatible shell is generally expected to first run the system-wide default login script /etc/profile, and then a user-specific ~/.profile if it exists.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .