Skip to main content
added 1 character in body
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

It is commonly known that env doedoes not allow passing of arguments - at least not reliably. There are various workarounds described here, but they felt to hacky and unreadable to me. That's why I developed an alternative, ex.

ex:

#!/usr/bin/env bash

# save the string given in the sheband line
shebang_line="$1"

# remove it from $@
shift

# execute 
# not quoting shebang_line is intentional
eval $(echo $shebang_line "$@")

test.py

#!/usr/local/bin/ex python "-i"
# quoting the arguments is possible
import sys

print('hello', sys.argv)

It works surprisingly well, but I don't like the eval-echo thing. I tried experimenting with arrays, but haven't found a solution. Any tips?

It is commonly known that env doe not allow passing of arguments - at least not reliably. There are various workarounds described here, but they felt to hacky and unreadable to me. That's why I developed an alternative, ex.

ex:

#!/usr/bin/env bash

# save the string given in the sheband line
shebang_line="$1"

# remove it from $@
shift

# execute 
# not quoting shebang_line is intentional
eval $(echo $shebang_line "$@")

test.py

#!/usr/local/bin/ex python "-i"
# quoting the arguments is possible
import sys

print('hello', sys.argv)

It works surprisingly well, but I don't like the eval-echo thing. I tried experimenting with arrays, but haven't found a solution. Any tips?

It is commonly known that env does not allow passing of arguments - at least not reliably. There are various workarounds described here, but they felt to hacky and unreadable to me. That's why I developed an alternative, ex.

ex:

#!/usr/bin/env bash

# save the string given in the sheband line
shebang_line="$1"

# remove it from $@
shift

# execute 
# not quoting shebang_line is intentional
eval $(echo $shebang_line "$@")

test.py

#!/usr/local/bin/ex python "-i"
# quoting the arguments is possible
import sys

print('hello', sys.argv)

It works surprisingly well, but I don't like the eval-echo thing. I tried experimenting with arrays, but haven't found a solution. Any tips?

Source Link
corvus_192
  • 549
  • 2
  • 8

Alternative to env that accepts arguments

It is commonly known that env doe not allow passing of arguments - at least not reliably. There are various workarounds described here, but they felt to hacky and unreadable to me. That's why I developed an alternative, ex.

ex:

#!/usr/bin/env bash

# save the string given in the sheband line
shebang_line="$1"

# remove it from $@
shift

# execute 
# not quoting shebang_line is intentional
eval $(echo $shebang_line "$@")

test.py

#!/usr/local/bin/ex python "-i"
# quoting the arguments is possible
import sys

print('hello', sys.argv)

It works surprisingly well, but I don't like the eval-echo thing. I tried experimenting with arrays, but haven't found a solution. Any tips?