2

I have a csh post-installation script that is executed from an rpm %post with its interpreter line set to #!/bin/csh (without the -f option). This should cause the file /etc/csh.cshrc to be read before executing the rest of the script, according to the man pages for both bsd-csh(1) and tcsh(1). I have certain system-wide environment variable and alias definitions in /etc/csh.cshrc that the script depends on to function properly. The expected behavior is that when csh executes the script, it picks up the system-wide definitions first, and the script then runs successfully.

This does work, at least initially under certain conditions. The csh script is always called the same way from within the rpm %post section. However, depending on how the application rpm install is executed, the script may not pick up the expected variables and error out, so it looks to me like it is probably not sourcing /etc/csh.cshrc at all. The install might be done in a root login shell, via sudo, over ssh, from cron, or initiated from some other process. At least one of these introduces some difference that prevents /etc/csh.cshrc from being sourced. I don't see anything in the man pages other than the -f option that would cause this.

2

It turns out that the difference is a missing $HOME environment variable. When $HOME is not defined in the environment, no startup files at all are sourced by csh or tcsh. The lack of a $HOME variable is interpreted by csh exactly the same as if it were given the -f command-line option.

This makes sense when considering the shell startup files ~/.cshrc and ~/.login in the user's home directory. If $HOME is not defined, then these user-specific files can't be loaded. But csh simply skips looking for all startup files, both system-wide and user-specific, when $HOME is not defined.

Defining $HOME to any valid directory before executing the csh script, whether it's the user's real home directory or even an empty temporary directory, makes csh source /etc/csh.cshrc as expected.

I have not found any documentation to explain or describe this feature, but the source code for both bsd-csh and tcsh confirm this behavior.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.