Rane Rane - 1 year ago 136
Bash Question

Option -l of exec shell command

Could you please clarify on the use of

option of
shell command. I didn't notice any difference when I ran
exec ls | cat
exec -l ls | cat

Answer Source

The -l option of exec adds a - at the beginning of the name of your command. For example:

exec -l diff | head

-diff: missing operand after '-diff'
-diff: Try '-diff --help' for more information.

Note the - everywhere before diff.

The point of all this? If you have a - before a command to start a shell it will act as a login shell. From man bash:

A login shell is one whose first character of argument zero is a -, or one started with the --login option.

Now, man exec states that:

If the -l option is supplied, the shell places a dash at the beginning of the zeroth argument passed to command. This is what login(1) does.

So exec -l bash will run bash as a login shell. To test this, we can use the fact that a login bash reads your ~/.bash_profile, so:

$ cat ~/.bash_profile 

echo yoho

If I start a login bash, the command echo yoho will be executed. Now to test with exec:

$ exec bash

Nothing is displayed, we are on a non-login shell.

$ exec -l bash

Here we have a login shell.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download