2

I am trying to prepare url, in which email address will be base64 encoded. However when I encode the email with base64 it returns emptry string, without base64 encode this works fine, what am I missing here?

here is shell script without base64 encode

#!/bin/bash

link='unsubscribe'
testVar="$link/""[email protected]"
echo "www.somedomain.com/$testVar/email/"

Output:

www.somedomain.com/unsubscribe/me@domain/email/

I am trying to encode email with base64 like this:

#!/bin/bash

link='unsubscribe'
testVar="$link/""[email protected]"|base64
echo "www.somedomain.com/$testVar/email/"

Output

www.somedomain.com/unsubscribe//email/

Notice the email did not get encoded, returned empty value

what I am missing here?

1 Answer 1

3

You are not passing the string contained in the variable test to base64 command, use Command substitution $(..) syntax to pass the string without newline to the command, using printf

#!/usr/bin/bash

link='unsubscribe'
test=$(printf "%s" "$link/""[email protected]" | base64)
echo "www.somedomain.com/${test}/email/"

produces an output as

www.somedomain.com/dW5zdWJzY3JpYmUvbWVAZG9tYWluLmNvbQ==/email/

Not relevant to your actual question, but I think as a general coding guideline, you should not name variables as test, as this can be easily confused with the test operator in bash. Would be good if you can add more context to what purpose the variable is used for, may be testVar, testStr or testRef.

Sign up to request clarification or add additional context in comments.

4 Comments

or just echo "www.somedomain.com/$(base64 <<< "$link/[email protected]")/email/" would be sufficient.
@123: If am not wrong, here-string, creates a new-line after the string, just like echo, try hexdump -c <<< "Hello World". Compare it with just hexdump -c < <( printf "%s" "Hello World" ). Hence your command might produce a different md5 string including the \n
Whoops, yeah your right, the point of the comment was that you could use command sub directly in the echo.
@Inian yes agreed to your comment related to naming variables properly, for asking question purpose I submitted the pseudo code, even then naming should have been proper, agreed :) thanks for your help, made edits in my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.