BASH Base64 Encode script not encoding right

I am trying to get base64 encoding to work and output to a variable in a bash script. The regular cli syntax is:

echo -ne "\ 0myuser@myhost.com \0mypass" | base64 

But when I try to put this in a variable in a script, it produces, but a very small encoding, so I know that it does not work. My code in the script:

 auth=$(echo -ne "\ 0$user@ $host\0$pass" | base64); 

I know this has something to do with quotes, but I tried a lot of things with different quotes, singles and backslashes without any problems.

Any thoughts?

EDIT: A bit more info. This should be output using the user / pass / host parameter above:

 AG15dXNlckBteWhvc3QuY29tAG15cGFzcw== 

But in the script, it outputs:

 LW5lIAo= 
+4
source share
2 answers

Ok, I will add this as an answer for the entries:

The problem was that /bin/sh is the default interpreter interpreter, which I assume was dash in this case.

The test script is used:

 #!/bin/bash user=myuser pass=mypass host=myhost.com auth=$(echo -ne "\ 0$user@ $host\0$pass" | base64); echo $auth 

Results:

 [51][00:33:22] vlazarenko@alluminium (~/tests) > echo -ne "\ 0myuser@myhost.com \0mypass" | base64 AG15dXNlckBteWhvc3QuY29tAG15cGFzcw== [52][00:33:42] vlazarenko@alluminium (~/tests) > bash base64.sh AG15dXNlckBteWhvc3QuY29tAG15cGFzcw== [53][00:33:46] vlazarenko@alluminium (~/tests) > dash base64.sh LW5lIAo= 
+11
source

Different versions of echo behave differently when you give them anything other than a simple line. Some interpret command options (for example, -ne ), and some simply print them as output; some interpret escape sequences in a string (even if the -e options are not specified), some of them do not work.

If you want consistent behavior, use printf instead:

 user=myuser pass=mypass host=myhost.com auth=$(printf "\0% s@ %s\0%s" "$user" "$host" "$pass" | base64) 

As a bonus, since the password (both username and host) are in simple lines, not in the format string, it will not try to interpret the escape sequences in them (does your real password have a backslash in it?)

+3
source

Source: https://habr.com/ru/post/1432590/


All Articles