Source a script from a URL in bash

bashurl

I gather this should work in bash:

source <(curl -s https://example.com/script.sh)

or

bash <(curl -s https://example.com/script.sh)

or

curl -s https://example.com/script.sh | source /dev/stdin

But it's not working for me. Downloading to a file, sourcing the file and then removing the file does work. I'm curious as to why none of the one liners are working though.

Best Answer

You are correct that your two first on-liners should be working according to bash process substitution semantics. In my testing (bash 3.2 on OS X 10.8.2), the second one does, while the first one does not.

In the case of your first one-liner, it looks like you may be running into one of the limitations of process substitution. Quoting the Wikipedia page on process substitution:

Process substitution has some limitations: the “files” created are not seekable, which means the process reading or writing to the file cannot perform random access; it must read or write once from start to finish. Programs that explicitly check the type of a file before opening it may refuse to work with process substitution, because the “file” resulting from process substitution is not a regular file.

– if source is a command that has difficulties with this (at least in bash 3.2), that would explain its failure to work with process substitution.

The second one-liner possibly just looks like it fails because it executes the code in a subshell rather than sourcing it. If you are expecting it to set aliases and functions, this won’t work, as these do not carry over to the parent shell when defined in a subshell.

The third one-liner doesn’t work because source does not process stdin – only files (see bash man page).