0

I have a bash shell script that performs a curl command that is made of variables:

# Variables
URL="http://$UN:$PW@localhost:8080/rest/v1"

The $UN and $PW are populated by a read command further within the script when a user logins in...

# Login
echo "Please enter your username and password."
read -p "Username: " UN
read -s -p "Password: " PW

lets assume at login the username is set to "alice" and the password set to something secure like "password".

The problem I have is that the $UN and $PW variables appear empty when the $URL is built i.e. the URL should look like this: http://alice:password@localhost:8080/rest/v1 but instead I'm seeing http://:@localhost:8080/rest/v1

if I echo the variables in the script I can see the UN and PW variables have been populated:

echo $UN

returns alice

echo $PW

returns password but if I

echo $URL

they're missing

I suspect this is something to do with the final / in the http:// but I could be wrong. Note I've tried changing inserting the variables into the $URL variable in different ways...

"http://${UN}:${PW}@localhost:8080/rest/v1"

"http://"$UN":"$PW"@localhost:8080/rest/v1"

"http://"${UN}":"${PW}"@localhost:8080/rest/v1"

...but the results are the same.

Any advice would be greatly appreciated!

3

You need to set the $URL variable after the $UN and $PW, like this:

echo "Please enter your username and password."
read -p "Username: " UN
read -s -p "Password: " PW
URL="http://${UN}:${PW}@localhost:8080/rest/v1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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