How to escape an @ sign in the password field of an smb:// URL

codersarepeople picture codersarepeople · Sep 1, 2010 · Viewed 12.5k times · Source

I'm trying to write a bash script in which I connect to a samba server, by getting the username and password, then saying $username:$password@SERVERNAME.

However, this will fail if the password has an @ in it. Is there a way to escape the @ out of the password in bash?

Thanks in advance

Update: I'm setting up this network printer

lpadmin -p PRINTER -v smb://$username:$password@SERVER -E

and it works except in the case that $password has an @ sign in it; the $username and $passwords variables are gotten from reading stdin

Answer

zwol picture zwol · Sep 1, 2010

Ah, no, this isn't actually a matter of quoting for Bash, but quoting for Samba. You have this:

lpadmin -p PRINTER -v smb://$username:$password@SERVER -E

which Bash dutifully expands to

lpadmin -p PRINTER -v smb://alice:passw@rd@SERVER -E

and then the Samba client library thinks the password ends at the first @ sign and it's supposed to connect to a server named rd@server, never mind that you can't actually put that name in the DNS.

lpadmin comes from CUPS, not from Samba (here is its manpage) and, reading through those docs a bit, I think you may be able to use this alternate syntax:

lpadmin -p PRINTER -U "${username}%${password}" -v smb://SERVER -E

I'm surprised escaping @ as %40 doesn't work, though. Looks like a bug in the samba client library to me.