1

I have a shell script that uses base64 to encode a value and store it in a variable.

encoded="$(cat $pathtofile|base64 -w 0)"

This worked until I ended up with a $pathtofile that had a special character in it. Now I'm trying to figure out how to quote the $pathtofile so that cat gets the right file. When I do this

encoded="$(cat '$pathtofile'|base64 -w 0)"

I end up with an error because it doesn't expand $pathtofile but prints it literally. I have tried several other combinations, but they all result in a misquoted path.

How can I end up with a quoted $pathtofile?

Daniel Watrous
  • 3,027
  • 2
  • 30
  • 45
  • Possible duplicate of [Difference between single and double quotes in Bash](https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash) – Benjamin W. Jul 13 '18 at 02:39

1 Answers1

3

Double quotes can be nested when you use $(...).

encoded="$(cat "$pathtofile" | base64 -w 0)"

For what it's worth, the outer set of quotes is optional. They're not needed in variable assignments. Remove them if you like.

encoded=$(cat "$pathtofile" | base64 -w 0)

Also, congratulations, you've won a Useless Use of Cat Award!

encoded=$(base64 -w 0 "$pathtofile")
John Kugelman
  • 330,190
  • 66
  • 504
  • 555