Bash scripting: Encoding string to base64 into a variable

Viewed 3386

I have a username and password variables. I want to encode them to base64 with the format of username:password.

#!/bin/bash
username=username
password=password

echo Encoding username/password...
echo $username:$password | base64

That works, but I'm not sure how to put the output in a variable instead of writing it to the console.

On a side note, why is the output different than a website like https://www.base64encode.org/?

1 Answers

Using $( ... ), store the result in a variable. And use -n to not include LF.

var=$(echo -n $username:$password | base64)
Related