How can I 'write' an environmental variable as input to a script?

时间:2015-07-28 22:11:06

标签: linux node.js shell command-line

When working with an interactive node script on the linux command line, how can I give it environmental variables as responses?

For instance, there is a certain value I very frequently have to type in as a response to parts of these scripts, and so I just set an environmental variable to contain it, i.e. my_var = "really long string of names".

But when the script prompts me, no matter what I do, it doesn't correctly interpolate the variable. It's as if I had just directly given it my_var or $(my_var) or `$my_var`. It always treats it as literal text.

1 个答案:

答案 0 :(得分:0)

An example using a shell variable in a node script :

bash shell :

$ x=foobar
$ nodejs <<EOF
var myvar = '$x'
console.log(myvar)
EOF

Result:

foobar