Henry Yang
Henry Yang

Reputation: 2583

What does `ruby -e "$(curl url)"` means?

What does this line from Homebrew mean?

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

I understand -e will include ruby code in command but I don't get the $() part. What's the dollar sign bracket doing here?

And very importantly, where can I find the documentation for this?

Upvotes: 5

Views: 1258

Answers (2)

Amadan
Amadan

Reputation: 198324

$(...) is Bash command substitution. It happens before the command is executed; it executes the command inside the parentheses and substitutes its output. For example,

echo "There are $(ls | wc -l) files in this directory"

will first execute ls | wc -l which will output e.g. 17; then echo "There are 17 files in this directory".

curl is a command-line utility that fetches the contents at an URL and outputs that content, by default. Thus, /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install) will first download the contents of https://raw.githubusercontent.com/Homebrew/install/master/install , then substitute it into the command line as the parameter of the -e option. Ruby will then execute it as Ruby code.

Upvotes: 10

user1934428
user1934428

Reputation: 22217

Your question is unrelated to Ruby; it is a shell question. Assuming that the shell running this command is either bash or ksh or Zsh, these shells replace an expression of the form $(COMMAND) by the standard output of this command. Hence in your case, the standard output of the curl command is executed as Ruby code.

Upvotes: -1

Related Questions