Skip to content
Advertisement

How to base64 encode image in linux bash / shell

I’m trying to base64 encode an image in a shell script and put it into variable:

test="$(printf DSC_0251.JPG | base64)"
echo $test
RFNDXzAyNTEuSlBH

I’ve also tried something like this:

test=`echo -ne DSC_0251.JPG | base64`

but still with no success.

I want to do something like this:

curl -v -X POST -d '{"image":$IMAGE_BASE64,"location":$LOCATION,"time_created":$TIMECREATED}' -H 'Content-type: text/plain; charset=UTF8' http://192.168.1.1/upload

I found this http://www.zzzxo.com/q/answers-bash-base64-encode-script-not-encoding-right-12290484.html

but still have had no success.

Advertisement

Answer

You need to use cat to get the contents of the file named ‘DSC_0251.JPG’, rather than the filename itself.

test="$(cat DSC_0251.JPG | base64)"

However, base64 can read from the file itself:

test=$( base64 DSC_0251.JPG )
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement