Yongqi Z
Yongqi Z

Reputation: 641

How to make the shebang use the value of an environment variable

I have a file named kong like this:

#!/usr/bin/env /usr/local/openresty/bin/resty 

setmetatable(_G, nil)
pcall(require, "luarocks.loader")

package.path = "./?.lua;./?/init.lua;" .. package.path

require("kong.cmd.init")(arg)

I can execute it directly, like kong start.

But I want to use environment variables to modify the resty path freely. Such as I set an environment variable export CURR_RESTY_PATH="XXXX", then modify the kong like this

#!/usr/bin/env ${CURR_RESTY_PATH}

I will get /usr/bin/env: ${CURR_RESTY_PATH}: No such file or directory if kong start

My question is how can I modify the #!/usr/bin/env to use environment variable?

Upvotes: 0

Views: 456

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295696

My question is how can I modify the #!/usr/bin/env to use environment variable?

You can't. There are awful, ugly hacks -- but in the real world (where we need things to be reliable, even if interpreters are rewritten in the future to use two-pass parsers that seek() back and reread their source files from the beginning), use a separate script.

For example, you might rename your old kong to kong.real, and then create a kong script that looks like this:

#!/usr/bin/env bash
exec "${CURR_RESTY_PATH:-/usr/local/openresty/bin/resty}" "${BASH_SOURCE}.real"

Upvotes: 2

Related Questions