Source a script from a URL in bash

Solution 1:

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).

Solution 2:

I don´t know what you like to do but if you like to download a script (or whatever) use.

curl -s -L https://example.com/script.sh -o script.sh

for more details see

man curl 

in terminal. -s silent, -L location of target including redirect to a new target, -o output file, if you don´t use it then curl uses the filename on the target (-o is often very useful).

To run the script use

./script.sh