0

I wanted to make zsh load ~/.profile at login. And I found this zsh-not-hitting-profile

Gilles's answers adding emulate sh -c '. ~/.profile' in ~/.zprofle does work.

But I wonder why Frank Terbeck make an addition to it:

[[ -e ~/.profile ]] && emulate sh -c 'source ~/.profile'

I am not very familiar with linux shell so I don't understand what he says:

And it's only active during the source. So you do not have to save the current option state in order to replay it again after sourcing.

  1. only active during the source? I need ~/.profile always need to be source , I can't get the meaning, because emulate sh -c 'source ~/.profile simply works everytime I logined.
  2. save the current option state in order to replay? what is the option state, why I need to replay?
Community
  • 1
  • 1
Mithril
  • 12,947
  • 18
  • 102
  • 153

1 Answers1

1

[[ -e ~/.profile ]] simply tests whether the file ~/.profile exists. This way you won't get an error doing source ~/.profile if the file isn't there.

"only active during the source" means that the -c option that you give to the emulate command doesn't change the options to the original shell process. It just uses that temporarily during the emulation of the source command.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks, by searching `bash check file exist` I understand `-e ~/.profile` usage , and http://stackoverflow.com/questions/13542832/what-is-the-difference-between-single-and-double-square-brackets-in-bash tell me what is the double square brackets mean. – Mithril Aug 16 '16 at 02:24
  • The original Bourne shell had a very limited external utility called `[` aka `test`; subsequent shells (predominantly) chose a similarly "syntax-looking" but different construct for introducing new built-in functionality, to preserve backwards compatibility but introduce new behavior. – tripleee Aug 16 '16 at 03:40
  • @tripleee That's explained in the question he linked to. – Barmar Aug 16 '16 at 03:41
  • Huh? I don't see an explanation of this syntax there. (Though he also posted the same *comment* there, too; flagged as too chatty.) – tripleee Aug 16 '16 at 03:45
  • @tripleee The top answer there says **Double [[]] are an extension to the standard [] and are supported by bash and other shells** – Barmar Aug 16 '16 at 03:47
  • Ah, sorry, I was looking at the SuperUser link in the question. – tripleee Aug 16 '16 at 03:49