Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to download a provisioning script that reads some configuration parameters via read, and execute it:

curl http://example.com/provisioning.sh | sh

The problem is, that the read command in the script is call with the -i parameter to provide a default:

read -p "Name: " -i joe name
echo $name

If I download the script, set the +x permission and run it, everything's fine.

If I run it with cat provisioning.sh | sh or sh provisioning.sh, it fails with:

read: Illegal option -i

Why wouldn't read support providing a default if it's run via sh?

But whatever, I'll remove the -i, being left with

read -p "Name: " name
echo $name

Now if I run the script via cat provisioning.sh | sh, it won't do anything. Why is that?

Ubuntu 14.04.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

When you pipe the output of curl into sh you're making the script text be standard input of the shell, which takes it in as commands to run. After that, there's nothing left to read. Even if it were to try, it wouldn't get anything from the terminal input, because it's not connected to it. The pipe has replaced standard input for the sh process.

The next problem is that read -i is not a POSIX sh feature, but rather an extension supported by bash. Ubuntu uses dash, a minimal POSIX-compliant shell with minimal extensions, as /bin/sh by default. That's why it rejects the -i option specifically (although it does support -p).

If you're using a more capable shell yourself, you can try something like:

bash <(curl http://example.com/provisioning.sh)

which creates a pipe for bash to read the output of curl from and provides it as the script file argument. In this case, the standard input of the script is still connected to the terminal, and read will work (but note the big caveat below the line).


I'll note also that "curl | sh" is generally frowned upon as an obvious security problem, but you know best the situation your script is in.

share|improve this answer
    
Thanks for the excellent answer! curl | sh is used by meteor.com ... –  Dan Dascalescu Jul 11 at 11:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.