bash: how to pass password containing special characters

谁都会走 提交于 2019-12-11 08:02:55

问题


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:


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.



来源:https://stackoverflow.com/questions/40492540/bash-how-to-pass-password-containing-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!