Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Source from string? Is there any way in shell?

Tags:

bash

shell

Example:

#!/bin/bash
source "$VARIABLE"

Is to retrieve values from a remote location using curl.

http://example.com/file.cnfg
like image 993
Robin pilot Avatar asked Mar 29 '15 00:03

Robin pilot


2 Answers

Executing downloads blindly is definitely not something you want to do casually.

You can execute the contents of a variable using eval, as in:

eval "$variable"

That's not something you want to do without thinking through the security implications either.

like image 51
rici Avatar answered Nov 13 '22 08:11

rici


You don't need or want a variable. The common way to do this is

curl http://example.com/file.cnfg | sh

If you really genuinely need to execute the code in the current shell,

source <(curl http://example.com/file.cnfg)

does that.

Caveats about security implications cannot be overstated. You need to understand what could go wrong if the server is malicious, hijacked, misconfigured, down, or just in a really grumpy mood.

You should be asking yourself "can I avoid using a variable" (and also, "can I avoid spawning an external process") and if the answer is yes, that's usually what you should do.

like image 42
tripleee Avatar answered Nov 13 '22 06:11

tripleee