How to decode an image string using base64 in command line?
Solution 1:
The utility base64
reads its input either from a file whose name is supplied as an argument, or from standard input. It never reads its input from a command line argument. In your case, to decode a string stored in a variable, you should supply the string on the standard input of base64
.
If you are using Bash, you may use a here-string:
base64 -d <<< "$myImgStr" > image2.jpg
If your shell does not accept here-strings, you can always use:
echo "$myImgStr" | base64 -d > image2.jpg
(Note the doublequotes around "$myImgStr"
. You should always doublequote variable expansions unless you have a good reason not to.)