Search code examples
bashvariableswget

I want to use wget with variable


I'm using a script to get the latest version of Prometheus. I'm able to do that:

v=$(curl -v --silent https://github.com/prometheus/node_exporter/releases/latest 2>&1 | grep "tag" | cut -d "v" -f2)

echo "latest version is $v"

latest version is 1.3.1

but on the next step for download with 'wget' and use a variable:

wget https://github.com/prometheus/node_exporter/releases/download/v$v/node_exporter-$v.linux-amd64.tar.gz

it fails:

https://github.com/prometheus/node_exporter/releases/download/v1.3.1%0D/node_exporter-1.3.1%0D.linux-amd64.tar.gz

I've tried to use wget with '' or use {} for variable like {$v}, but I got same result. Can someone help me?


Solution

  • Add | tr -d '\r' to your curl command to strip the carriage returns as follows:

    #!/bin/bash
    
    v=$(curl -v --silent https://github.com/prometheus/node_exporter/releases/latest 2>&1 | grep "tag" | cut -d "v" -f2 | tr -d '\r')
    echo "latest version is $v"
    
    wget https://github.com/prometheus/node_exporter/releases/download/v$v/node_exporter-$v.linux-amd64.tar.gz