bash: how to pass password containing special char

2019-07-26 23:11发布

I have a bash script to download data from a website. For this I need to pass the username and password using a java client for the website. My password has a special character that is causing problems. I have tried the usual solutions but none are working.

#!/bin/sh
#$ -cwd

MYPASSWORD='password&123'

java -jar client.jar -p username $MYPASSWORD file.gz

Tried using \ either directly in the command or using the variable. But that didnt work. Login failed!

MYPASSWORD=password\&123
java -jar client.jar -p username $MYPASSWORD file.gz

Tried "" or ''. Login failure.

MYPASSWORD='password&123'
java -jar client.jar -p username "\"$MYPASSWORD\"" file.gz

Also tried it, no luck. MYPASSWORD='password'"&"'123'

Any suggestions would be most helpful. Resetting the password on this website also seems problematic.

1条回答
Summer. ? 凉城
2楼-- · 2019-07-27 00:13

Just quote the parameter.

mypassword='password&123'
java -jar client.jar -p username "$mypassword" file.gz

The quotes are only there to keep the shell from performing any further processing of the value of mypassword after the parameter is expanded. You don't actually pass quotes to your program.

查看更多
登录 后发表回答