- How can I decode a base64 string from the command line?
- 8 Answers 8
- You must log in to answer this question.
- Bash base64 encode and decode
- Syntax:
- base64 [OPTION] [INFILE] [OUTFILE]
- Options:
- Example#1: Encoding text data
- Example#2: Decoding text data
- Example#3: Encoding text file
- Example#4: Decoding text file
- Example#5: Encoding any user-defined text
- Example#6: Checking user validity by decoding text
- Conclusion:
- References:
- About the author
- Fahmida Yesmin
- How to base64 encode and decode from command-line
- How to base64 encode on Ubuntu, Debian, OSX, and Red Hat
- Why Base64 Encode Data
- Base64 is not Encryption
- Base64 Encoding a String
- Base64 Encoding a File
- Decoding Strings
- Decoding Files
- Conclusion
How can I decode a base64 string from the command line?
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.)
8 Answers 8
Just use the base64 program from the coreutils package:
echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode
Or, to include the newline character
echo `echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | base64 --decode`
echo QWxhZGRpbjpvcGVuIHNlc2FtZQ== | «C:\Program Files\Git\usr\bin\base64» —decode 2> nul > example.txt On Windows with git’s base64.
@January It is not Just use , because many people know about the base64 program – but as one can’t just insert a string as command line option, it is hard to get the syntax right for users who touch the CLI only once in a while.
openssl can also encode and decode base64
EDIT: An example where the base64 encoded string ends up on multiple lines:
$ openssl enc -base64 QW5kIGlmIHRoZSBkYXRhIGlzIGEgYml0IGxvbmdlciwgdGhlIGJhc2U2NCBlbmNv > ZGVkIGRhdGEgd2lsbCBzcGFuIG11bHRpcGxlIGxpbmVzLgo= > EOF And if the data is a bit longer, the base64 encoded data will span multiple lines.
Thanks to Philippe’s answer, you need to add -A for long base64 strings otherwise openssl will return nothing, see askubuntu.com/a/271676/305568
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, 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.
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.
Add the following to the bottom of your ~/.bashrc file:
Now, open a new Terminal and run the command.
decode QWxhZGRpbjpvcGVuIHNlc2FtZQ==
This will do exactly what you asked for in your question.
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 | .
I did comment base64 command line in http://wiki.opensslfoundation.com/index.php?title=Command_Line_Utilities. So I issue a Warning when using openssl base64 decoding :
warning base64 line length is limited to 64 characters by default in openssl :
to be able to decode a base64 line without line feed that exceed 64 characters use -A option :
This is anyway better to actualy split base64 result in 64 characters lines since -A option is BUGGY ( limit with long files ).
openssl wiki documentation was moved here wiki.openssl.org/index.php/… then a rewrtie of did lost this -A usage and 64 characters limits.
perl -MMIME::Base64 -ne 'printf "%s\n",decode_base64($_)' )" data-controller="se-share-sheet" data-se-share-sheet-title="Share a link to this answer" data-se-share-sheet-subtitle="" data-se-share-sheet-post-type="answer" data-se-share-sheet-social="facebook twitter " data-se-share-sheet-location="2" data-se-share-sheet-license-url="https%3a%2f%2fcreativecommons.org%2flicenses%2fby-sa%2f3.0%2f" data-se-share-sheet-license-name="CC BY-SA 3.0" data-s-popover-placement="bottom-start">Share )" title="">Improve this answer answered Jul 2, 2015 at 20:22 A.B.A.B. 88.8k 21 gold badges 245 silver badges 321 bronze badges