3

How do I support filenames with spaces in the following command?

echo "$(perl -MMIME::Base64 -e 'open F, shift; @lines=<F>; close F; print MIME::Base64::encode(join(q{}, @lines))' $filename)"

I tried the following which didn't seem to work:

  1. echo ... \"$filename\")
  2. echo '$(... "open..." "$filename")'
  3. echo $(...'open ... "$filename")
forthrin
  • 2,289

1 Answers1

5
echo "$(perl -MMIME::Base64 -0777 -pe '$_=MIME::Base64::encode$_' < "$filename")"

I assume echo is an example here. If not, this:

perl -MMIME::Base64 -0777 -pe '$_=MIME::Base64::encode$_' < "$filename"

would be equivalent.

Note that some systems have a base64 command:

base64 < "$filename"

Or if openssl is installed:

openssl base64 < "$filename"
  • Brilliant response! Not only did you solve the problem, but provided several simple ways to achieve the same thing, all of which work. Actually echo was part of the original code, but I found it to be unnecessary after all. PS! Since this is going into an email, one should do base64 -b 76. – forthrin Aug 26 '18 at 13:50