简体   繁体   中英

How can I execute a shell script on linux directly from a URL if it needs to change environment variables?

Goals:

  • One-liner script execution
  • Changes environment variables in the caller

Is this possible?

I know you can do something like curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash , but this doesn't allow changing environment variables in the calling environment.

You can use process substitution together with the . ( source ) command:

. <(curl https://www.server.com/env.sh)

This would import all variable, alias and function definitions from env.sh into the scope of the running shell. However, it would not implicitly export the variables to the environment. To achieve this export has to be explicitly called in env.sh or the calling shell. Like this:

export FOO="test"

Ordinary environment variables will never be exported to child process. You either have to explicitely export them, or tell the calling shell to provide a given environment variable by prepending it's definition to the call:

curl -o- ${URL2SCRIPT} | FOO="test1" bash

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM