bash: source from URL

7

3

In addition to my own computer, I sometimes use an Ubuntu cluster at my school. Rather than manually keep my .bashrc's in sync, I would like to make the school cluster's .bashrc source my personal .bashrc from DropBox via a URL. However, when I naively try source http://myurl, I just get an error: http://myurl: No such file or directory. How can I can get bash to source from a script located online?

Worst case, I could curl to a named pipe and source that. Is there anything more elegant?

AlcubierreDrive

Posted 2011-03-09T13:45:20.167

Reputation: 357

Answers

12

You can use process substitution with source:

source <(curl -s http://example.com/foo)

Note: I consider directly running code retrieved over the internet to be a serious security risk. It's probably less risky if this is done over an internal network (depending on its overall security).

Paused until further notice.

Posted 2011-03-09T13:45:20.167

Reputation: 86 075

1Works on Linux, fails on OSX. Writing to a tempfile for OSX instead. – Edward Anderson – 2014-09-08T20:29:50.280

@nilbus: According to this, you might try source <(curl -s http://example.com/foo | iconv -f windows-1251)

– Paused until further notice. – 2014-09-08T20:57:02.073

That got rid of the error, but the script didn't run still on OSX. – Edward Anderson – 2014-09-09T00:31:06.120

The part of this that jumps out at me is the fact that I can set a variable from^H^H^H^Hin the main script and call that variable without having to redeclare it in the sourced script. curl <file> | /bin/bash won't allow that without exporting the variables. – dafydd – 2015-12-05T04:32:10.873

fail on bash, success on ksh – Miao1007 – 2016-12-13T11:40:59.190

@Miao1007: It works in Bash. You provide no information to use to diagnose why you are having a failure. – Paused until further notice. – 2016-12-13T16:50:42.973

@Dennis Williamson: fixed by using source /dev/stdin <<< "$(curl -s http://xxx.sh)", it works fine on both! – Miao1007 – 2016-12-14T10:53:42.683

@EdwardAnderson The reason this syntax fails in OSX is because OSX is using bash 3.2, which does not support process substitution for source. It only works for bash 4.? https://stackoverflow.com/questions/32596123/why-source-command-doesnt-work-with-process-substitution-in-bash-3-2/32596626#32596626

– wisbucky – 2019-09-25T21:20:07.303

Clever! Good point though that an attacker could easily feed me any code they want. – AlcubierreDrive – 2011-03-09T14:49:53.050

-1

Can't you just use

curl http://example.com/whatever.sh | bash 

John Burton

Posted 2011-03-09T13:45:20.167

Reputation: 441

That works and it's what I'm currently doing, but I was wondering if there's a more elegant way. But maybe this is the best, so if nothing turns up soon I'll accept your answer. – AlcubierreDrive – 2011-03-09T14:12:58.620

Also, note that this is going in my .bashrc, so technically your solution is infinite recursion. ;) But I know what you meant. – AlcubierreDrive – 2011-03-09T14:45:45.230

5Piping commands to bash will execute them in a separate process, which makes this method unusable for executing bashrc. – user1686 – 2011-03-09T15:15:41.430

2Depends on what the commands are - but yes, sorry I agree – John Burton – 2011-03-09T16:24:04.603