0

I'm trying to fetch the value of a currency from remote api, so I can later run it through some calculation. However, I cannot assign this value to a variable.

#!/usr/bin/env zsh

result = $(curl -s -X GET "https://openexchangerates.org/api/latest.json?app_id=SOME_KEY" | jq '.rates.GBP')

echo $result

This results in :

> ./script.sh:5: command not found: result
BVtp
  • 2,308
  • 2
  • 29
  • 68
  • FYI `bash` and `zsh` are two different shells. Your shebang indicates you are using the latter, not the former. – chepner Jun 04 '20 at 17:43

1 Answers1

2

You can't have spaces around an equals sign in a Bourne-like shell (e.g., sh, bash, zsh). What happens is that when zsh sees this line:

result = $(curl -s -X GET "https://openexchangerates.org/api/latest.json?app_id=SOME_KEY" | jq '.rates.GBP')

it thinks that result is the name of a command and that the equals sign and what follows it are the arguments of that command. To avoid this, just do this:

result=$(curl -s -X GET "https://openexchangerates.org/api/latest.json?app_id=SOME_KEY" | jq '.rates.GBP')

ETA: I do notice that the line number for the error is 5, not 3 as I would expect. I don't know if that's because of CR/LF line ending issues, if there's something missing from the script that you showed us, or whatnot.

jjramsey
  • 1,131
  • 7
  • 17
  • wow, I feel silly now, can't believe I wasted an hour because of spaces.. Thank you! (and as for the line count, it is in fact just an incorrect copy-paste) – BVtp Jun 04 '20 at 17:47