Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash scripting: Encoding string to base64 into a variable [duplicate]

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/?

like image 803
Gilbert Williams Avatar asked Jul 21 '26 09:07

Gilbert Williams


1 Answers

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

var=$(echo -n $username:$password | base64)
like image 117
etsuhisa Avatar answered Jul 23 '26 00:07

etsuhisa