bash, how to dot source a downloaded file (using curl) into bash

627 Views Asked by At

I have .sh file that I would like to dotsource into my running environment. This does not work:

curl -s https://raw.githubusercontent.com/bla/bla/master/stuff.sh | bash

The above does not work, i.e. The script runs, but the environment variables and things inside stuff.sh are not dotsourced into the running environment. I also tried:

. curl -s https://raw.githubusercontent.com/bla/bla/master/stuff.sh | bash
curl -s https://raw.githubusercontent.com/bla/bla/master/stuff.sh | bash source
curl -s https://raw.githubusercontent.com/bla/bla/master/stuff.sh | source bash

All fail. Would appreciate knowing how this can be done?

2

There are 2 best solutions below

4
On BEST ANSWER

I am not a bash expert, but if you are willing to accept some drawbacks, the easiest method to do that is without pipes. I believe that it should be possible when you separate download and sourcing:

prompt># curl -s https://raw.githubusercontent.com/bla/bla/master/stuff.sh > ./stuff.sh
prompt># . ./stuff.sh

From the bash manual (man bash), in the chapter about the builtin source command:

Read and execute commands from filename [...]

There is no mentioning about standard input as a possible source for the commands which should be sourced.

However, as hanshenrik stated in his answer, you always can use process substitution to create a temporary (and invisible on the file system) file which you can feed to source. The syntax is <(list), where <(list) is expanded to a unique file name chosen by bash, and list is a sequence of commands whose output is put into that file (the file does not appear on the file system, though).

Process substitution is documented in the bash manual (man bash) in a paragraph under that exact caption.

2
On

try

source <(curl -s https://raw.githubusercontent.com/bla/bla/master/stuff.sh)

i tried doing

curl -s https://raw.githubusercontent.com/bla/bla/master/stuff.sh | source /dev/stdin

but that didn't work for some reason, no idea why (anyone knows?)