Could you please clarify on the use of -l
option of exec
shell command. I didn’t notice any difference when I ran exec ls | cat
and exec -l ls | cat
.
Advertisement
Answer
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 executes the file ~/.bash_profile
, so:
$ cat ~/.bash_profile #!/bin/sh printf "I am a login shell!n"
If I start a login bash, the command printf "I am a login shell!n"
will be executed. Now to test with exec
:
$ exec bash $
Nothing is displayed, we are on a non-login shell.
$ exec -l bash I am a login shell! $
Here we have a login shell.