1

With bash, for example, one can pass the --norc --noprofile flags to prevent the reading of shell initialization files.

What's the equivalent for sh?

If there isn't one for sh, is there at least one for dash?

(I've tried hacks based on temporarily unsetting HOME, but they seem to be too fragile for production work.)

kjo
  • 15,339
  • 25
  • 73
  • 114
  • 3
    I assume you mean an instance of a login shell; non-login shells read no files by default (so --norc wouldn't be needed, and is in fact ignored by bash when started with the name sh). I don't think there is anyway to prevent a login shell from sourcing /etc/profile, though. – chepner Sep 10 '15 at 20:42
  • 2
    Read your shell's man page, section Invocation. How does it detect that it is a non-interactive shell? – ott-- Sep 10 '15 at 21:00

2 Answers2

4

sh only loads .profile if it's a login shell, i.e. if it's invoked with argument 0 starting with -. So if you don't want /etc/profile and ~/.profile to be read, invoke the shell with the default argument 0 (the name of the executable).

If something is executing the shell as a login shell, you don't have control over arguments. If not, the shell isn't a login shell.

Apart from the files that are loaded if the shell is a login shell, there's one other file that some sh implementations load: the file indicated in the ENV environment variable. If you're invoking a shell and don't want to load any file, make sure that ENV is unset.

1

Doesn't look like it. You can find the code at the link below. Seems there are no flags to control this behaviour.

http://git.kernel.org/cgit/utils/dash/dash.git/tree/src/main.c#n147

alienth
  • 2,197
  • 1
    A useful link is indeed http://git.kernel.org/cgit/utils/dash/dash.git/tree/src/options.c , and it shows that xargv[0] is checked for the starting hyphen. – Incnis Mrsi Sep 10 '15 at 21:40