Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an environment variable in the shebang (#!) of a script that will be run by Bash?

Tags:

bash

unix

Or, why doesn't this work, assuming you correctly set the file as executable? (On my system it just hangs, but can be killed with ^C.)

#!/usr/bin/env TEST=TEST python
print('hello')

While this does:

#!/usr/bin/env python
print('hello')

As does this:

[fred@pc build]$ /usr/bin/env TEST=TEST python hello.py 
hello
like image 536
Fredrick Brennan Avatar asked Jan 26 '23 06:01

Fredrick Brennan


1 Answers

Since version 8.30 env has the option -S, which make things work also in linux:

Use

#!/usr/bin/env -S TEST=TEST python
like image 87
Martin Ziegler Avatar answered Jan 29 '23 07:01

Martin Ziegler