1

Trying to download latest SBT version from GitHub:

version="$(curl -vsLk https://github.com/sbt/sbt/releases/latest 2>&1 | grep "< Location" | rev | cut -d'/' -f1 | rev)"

version is set to v1.1.0-RC2

Then attempting to download the .tar.gz package:

curl -fsSLk "https://github.com/sbt/sbt/archive/${version}.tar.gz" | tar xvfz - -C /home/myuser

However, instead of the correct URL:

https://github.com/sbt/sbt/archive/v1.1.0-RC2.tar.gz

Somehow the version string is interpreted as a command(?!), resulting in:

.tar.gzttps://github.com/sbt/sbt/archive/v1.1.0-RC2

When I manually set version="v1.1.0-RC2", this doesn't happen.

Thanks in advance!

vivri
  • 885
  • 2
  • 12
  • 23
  • It's absolutely part of the string -- it's just a string that contains a sequence that sends the cursor back to the front of the line, so when it's printed, it *looks* like it's overwriting the command. Doesn't mean anything like that is actually happening. – Charles Duffy Dec 19 '17 at 18:43
  • Thank you @BenjaminW.! adding a ` | sed 's/\r//g' ` at the end fixed it. – vivri Dec 19 '17 at 18:43
  • BTW, this is literally the very first thing in the "before asking about problematic code" section in the [`bash` tag wiki](https://stackoverflow.com/tags/bash/info). – Charles Duffy Dec 19 '17 at 18:44

1 Answers1

1

You should use -I flag in curl command and a much simpler pipeline to grab version number like this:

curl -sILk https://github.com/sbt/sbt/releases/latest |
awk -F '[/ ]+' '$1 == "Location:"{sub(/\r$/, ""); print $NF}'

v1.1.0-RC2

Also note use of sub function to strip off \r from end of line of curl output.

Your script:

version=$(curl -sILk https://github.com/sbt/sbt/releases/latest | awk -F '[/ ]+' '$1 == "Location:"{sub(/\r$/, ""); print $NF}')

curl -fsSLk "https://github.com/sbt/sbt/archive/${version}.tar.gz" | tar xvfz - -C /home/myuser
anubhava
  • 761,203
  • 64
  • 569
  • 643