0

Up until I recently upgraded to macOS 13 (Ventura) I could run this Bash command to convert a JPEG image into a Base64 string in a script I occasionally use:

IMAGE_BASE64=$(base64 -b0 /path/to/image.jpg);

The resulting IMAGE_BASE64 value could be used as the variable $IMAGE_BASE64 in my script.

But I recently tried to use this script again in macOS 13 (Ventura) and this base64 command was failing with the following error:

base64: invalid argument -b0
Usage:  base64 [-hDd] [-b num] [-i in_file] [-o out_file]
  -h, --help     display this message
  -Dd, --decode   decodes input
  -b, --break    break encoded string into num character lines
  -i, --input    input file (default: "-" for stdin)
  -o, --output   output file (default: "-" for stdout)

Switching the command to use -i seems to solve the issue:

IMAGE_BASE64=$(base64 -i /path/to/image.jpg);

But what is the difference between the two commands?

What was -b0 doing previously prior to macOS 13 (Ventura) that -i is handling now in macOS 13 (Ventura)?

1 Answer 1

1

Will answer my own question.

Two “gotchas” grabbed me here and hopefully this answer will help someone in a similar position in the future:

  1. The -b0 needs to be set to -b 0 nowadays.
  2. The -i is now needed to indicate what the input file is.

So simply setting -b0 in macOS 13 (Ventura) would never work. It needs to be -b 0 -i. More details below.

The -b0 relates to line breaks and is the equivalent of --break=0 which means that when that option is set to 0 line breaks are disabled which is what one wants for Base64 encoded binary image data; no line breaks!

Details found here in this Mac OS X 10.7 man page for base64 as well as the man page for base64 which states:

The following options are available for base64:

-b count, --break=count
       Insert line breaks every count characters.  The default is 0,
       which generates an unbroken stream.

Knowing that, this command breaks in macOS 13 (Ventura):

IMAGE_BASE64=$(base64 -b0 /path/to/image.jpg);

But this slight variant, where there is a space between -b and 0 as well as the -i for the input file, would work:

IMAGE_BASE64=$(base64 -b 0 -i /path/to/image.jpg);

But the -i for the input file now seems to be required and no longer just implied by the path being the last item in the command.

What a subtly confusing headache.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .