Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash shebang option -l

Tags:

I use a script, test.sh, written by someone else, the begins with a bash shebang:

#!/bin/bash -l ... echo TEST: $TEST 

From what I could see, this has an effect on variables used inside the script:

  • if I run TEST=hey ./test.sh, I can see TEST: hop, hop being the value of variable TEST in my .bash_profile
  • this is the same if I export TEST=hey before running the script
  • but if I remove the -l flag, the same command returns TEST: hey, as I would have expected

Can someone please explain this behaviour ? The help of bash did not... help.

like image 706
Emmanuel Avatar asked Dec 10 '13 16:12

Emmanuel


People also ask

Is bash shebang necessary?

If you are executing this script from the Bash shell, then it is not necessary to specify the Bash interpreter with the shebang. However, it is certainly recommended that you specify it anyway. If someone executes your script from a different shell, then the script could be parsed with an unintended interpreter.

What is '- E option to bash?

by Aqsa Yasin. Set –e is used within the Bash to stop execution instantly as a query exits while having a non-zero status. This function is also used when you need to know the error location in the running code.

How do I get to shebang line?

The shebang is a special character sequence in a script file that specifies which program should be called to run the script. The shebang is always on the first line of the file, and is composed of the characters #! followed by the path to the interpreter program.

Do you need shebang in shell script?

I strongly recommend you to never write neither run a shell script without a #! shebang! As we said, the shebang tells to the kernel which interpreter is to be used to run the commands present in the file.


1 Answers

The -l option (according to the man page) makes "bash act as if it had been invoked as a login shell". Login shells read certain initialization files from your home directory, such as .bash_profile. Since you set the value of TEST in your .bash_profile, the value you set on the command line gets overridden when bash launches.

like image 190
dg99 Avatar answered Sep 18 '22 15:09

dg99