Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decode multiple base64 strings from standard input

Tags:

bash

macos

I have a file that contains multiple base64 encoded strings split by a new line character:

Y2F0Cg==
ZG9nCg==
ZmlzaAo=

When I execute: cat b64s.txt | base64 -D I expect to see:

cat
dog
fish

but I only see:

cat

What's an easy way to execute base64 -D for each line read from standard in?

like image 693
Breedly Avatar asked Nov 16 '25 09:11

Breedly


2 Answers

while read line; do base64 -D <<< "$line"; done < b64s.txt

For what it's worth, on my Linux system with GNU coreutils 8.28 the decode flag is -d and it automatically decodes a file with multiple strings:

$ base64 -d b64s.txt
cat
dog
fish
like image 165
John Kugelman Avatar answered Nov 18 '25 15:11

John Kugelman


The stock /usr/bin/base64 in macOS will only decode the first string from your input file. But base64 decoders vary wildly, there's been a fair amount of discussion on the topic even her in SO.

Python is installed by default in macOS, and the Python 2.7 interpreter that probably lives at /usr/bin/python will likely behave the way you want.

$ uname -a
Darwin ghoti.local 15.6.0 Darwin Kernel Version 15.6.0: Tue Jan 30 11:45:51 PST 2018; root:xnu-3248.73.8~1/RELEASE_X86_64 x86_64
$ cat i
Y2F0Cg==
ZG9nCg==
ZmlzaAo=
$ python -m base64 -d < i
cat
dog
fish
like image 36
ghoti Avatar answered Nov 18 '25 13:11

ghoti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!