bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#20553: 'echo -e' does not escape backslash correctly


From: Pádraig Brady
Subject: bug#20553: 'echo -e' does not escape backslash correctly
Date: Mon, 11 May 2015 23:14:02 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0

tag 20553 notabug
close 20553
stop

On 11/05/15 22:50, Jo Drexl (FFGR-IT) wrote:
> Hi guys,
> I had to write a Windows bat file for twentysomething users and - as Linux 
> geek - wrote a small Bash script for it. The code in question is as follows:
> 
> echo -e "net use z: \\\\srv\\aqs /persistent:no /user:%USERNAME% $BG_PASSWD\r"
> 
> I expected the created files being as such:
> net use z: \\srv\aqs /persistent:no /user:%USERNAME% user-password
> 
> but got:
> net use z: \srvqs /persistent:no /user:%USERNAME% user-password
> 
> Without the parameter e the code for a working file had to be:
> echo "net use z: \\srv\aqs /persistent:no /user:%USERNAME% $BG_PASSWD"
> but missed the Windows-newline carriage return (\r) - adding that without 
> changing the echo command didn't print out the carriage return character but 
> the literal backslash-R, as the default behaviour (parameter E) indicates.
> 
> Long story short: Escaping doesn't work correctly, either it's the Bash 
> interfering (but only escaping the double backslashes, and afterwards echo -e 
> escaping everything correctly), or the double backslash escape doesn't work 
> at all. It CAN be healed by using single quotes, but then variable expansion 
> doesn't work, or by using a variable workaround:
> 
> BG_SHARE='\\\\srv\\aqs'
> echo -e "net use z: $BG_SHARE /persistent:no /user:%USERNAME% $BG_PASSWD\r"
> 
> but I'm pretty positive this is a workaround, not the way it should work.
> 
> Maybe you dig into it, if you have the time. Since I found nothing worthy 
> googleing the issue, it seems like a quite uncommon situation I'm in.
> 
> BR
> Jo
> 
> P.S.: It's a standard Debian Wheezy without clickery GUI. No funny changes to 
> the default behaviour of Bash or the login shell.


The shell _and_ echo -e are processing the \'s
To get the shell to stop you can use single quotes like:

 echo -e 'net use z: \\\\srv\\aqs /persistent:no /user:%USERNAME% $BG_PASSWD\r'

Or to leave to shell to also process \r you can:

 echo $'net use z: \\\\srv\\aqs /persistent:no /user:%USERNAME% $BG_PASSWD\r'

Note echo is not portable to other systems, and if that's required,
printf(1) is a better option, though that will have different
quoting again due to the % chars etc.

cheers,
Pádraig.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]