10

I have seen this command in a few different shell scripts:

exec $SHELL -l

...usually as an alternative to:

source ~/.profile

What exactly is exec $SHELL -l doing?

2 Answers 2

10

The exec command replaces the current process image - the executable or program - with a new one, named as the argument to exec. If $SHELL contains the name of an executable, as it usually does, exec will spin that exe up in place of the running shell.

HOWEVER, that's a very different action than just using "source" to read in a file of commands. The source command read the named file and executes the contents line by line, as though typed at the command line of the current shell.

8

It's invoking your shell ($SHELL) as a login shell.

excerpt from Bash man page

-l       Make bash act as if it had been invoked as a login shell 
         (see INVOCATION below).

I suggest reading through the INVOCATION section of the man page for more information.

TL;DR;

Bottom line is that it tells Bash which files to source when it invokes. Either the $HOME/.bash_profile (-l) or the $HOME/.bashrc (-i).

References

3
  • Pay it forward 8-)
    – slm
    Sep 17, 2013 at 18:02
  • Ah, so it sort of takes care of inconsistencies in bash profile file names?
    – Andrew
    Sep 17, 2013 at 18:04
  • 1
    You're launching a new shell. So if you've made changes to a shell config, running the $SHELL -l command would "apply" those changes. Running source ~/.profile would do so similarly without starting a new shell, except any variables or functions in your config that are not overwritten will remain set as they were initially.
    – jktravis
    Sep 17, 2013 at 18:08

You must log in to answer this question.

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