Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to write a bash script to decode a base64 string. For example I type decode QWxhZGRpbjpvcGVuIHNlc2FtZQ== and it prints Aladdin:open sesame and returns to the prompt.

So far I have tried a simple bash file containing python -m base64 -d $1 but this command expects a filename not a string. Is there another non-interactive command (not necessarily in a Python module) that I can run from the command line to achieve this, without having to install any extra packages? (Or if I do, something super-minimal.)

share|improve this question
up vote 210 down vote accepted

Just use the base64 program from the coreutils package:

echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode

Or, to include the newline character

echo `echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode`

output (includes newline):

Aladdin:open sesame
share|improve this answer
Or: echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode && echo – Greg Chabala Sep 4 '14 at 16:08
Or: base64 -d <<< QWxhZGRpbjpvcGVuIHNlc2FtZQ== – jmk Jul 29 '15 at 11:23

openssl can also encode and decode base64

$ openssl enc -base64 <<< 'Hello, World!'
$ openssl enc -base64 -d <<< SGVsbG8sIFdvcmxkIQo=
Hello, World!

EDIT: An example where the base64 encoded string ends up on multiple lines:

$ openssl enc -base64 <<< 'And if the data is a bit longer, the base64 encoded data will span multiple lines.'
$ openssl enc -base64 -d << EOF
> QW5kIGlmIHRoZSBkYXRhIGlzIGEgYml0IGxvbmdlciwgdGhlIGJhc2U2NCBlbmNv
> ZGVkIGRhdGEgd2lsbCBzcGFuIG11bHRpcGxlIGxpbmVzLgo=
And if the data is a bit longer, the base64 encoded data will span multiple lines.
share|improve this answer
Thanks to Philippe's answer, you need to add -A for long base64 strings otherwise openssl will return nothing, see – morloch Oct 2 '14 at 6:02
@morloch or just avoid removing the newlines from the base64 encoded data, and it works as expected... – geirha Oct 2 '14 at 7:56
I would not consider coreutils an "additional" package containing programs like ls, mkdir, cp, mv, and chmod. I doubt you can do anything useful with your machine without it. – vidstige Jan 15 '15 at 20:14
@vidstige, that's true. I don't know why I was under the impression that base64 was not installed by default; that is totally not the case. – geirha Jan 16 '15 at 14:26
While this is the ubuntu stack exchange, using openssl has the advantage over standard base64 of working in Git Bash on Windows, at least the older 1.8.1 Git Bash version I have installed. – willkil Jun 16 '15 at 23:05

Here you go!

Add the following to the bottom of your ~/.bashrc file:

decode () {
  echo "$1" | base64 -d ; echo

Now, open a new Terminal and run the command.

decode QWxhZGRpbjpvcGVuIHNlc2FtZQ==

This will do exactly what you asked for in your question.

share|improve this answer

With your original dependencies it is possible to do this with a minor modification to your original script:

echo $1 | python -m base64 -d

If you don't pass a file name, that python module reads from the standard input. To pipe the first parameter into it you can use echo $1 |.

share|improve this answer
Also: python -m base32 filename – kenorb Nov 10 '12 at 22:57
this does not work :) but use .b32* methods – rzr Feb 16 '14 at 11:11

I did comment base64 command line in So I issue a Warning when using openssl base64 decoding :

 openssl base64 -e <<< 'Welcome to openssl wiki'


openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kK'

Welcome to openssl wiki

warning base64 line length is limited to 64 characters by default in openssl :

 openssl base64 -e <<< 'Welcome to openssl wiki with a very long line
 that splits...'


openssl base64 -d <<< 'V2VsY29tZSB0byBvcGVuc3NsIHdpa2kgd2l0aCBhIHZlcnkgbG9uZyBsaW5lIHRoYXQgc3BsaXRzLi4uCg=='


to be able to decode a base64 line without line feed that exceed 64 characters use -A option :

openssl base64 -d -A <<<

Welcome to openssl wiki with a very long line that splits...

This is anyway better to actualy split base64 result in 64 characters lines since -A option is BUGGY ( limit with long files ).

share|improve this answer
@Seth please reset blocks correctly, your edit merged openssl command with my interleaved comments. – philippe lhardy Mar 23 '13 at 19:41
Can you fix it? I can't differentiate between the two... I'll try, but you might need to help me out ;-) – Seth Mar 23 '13 at 19:43
well just have to take a look on… where i managed to use wiki syntax better ( search bas64 in this page ). – philippe lhardy Mar 23 '13 at 20:24

Using perl

perl -MMIME::Base64 -ne 'printf "%s\n",decode_base64($_)' <<< "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="

Or the same with python

python -m base64 -d <<< "QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
share|improve this answer

Just to add another way to do it:

emacs -Q --batch  -eval '(princ (base64-encode-string (read-string ": ")))'
share|improve this answer

protected by Community Jul 2 '15 at 20:26

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.