0

To avoid \n:
using "printf" rather than "echo"

Set String

IFS='' read -r -d '' V <<'EOF'
'^+%&/%+^
EOF

Test w/ echo ( works fine )

echo "$V"
'^+%&/%+^

Test w/ printf ( fails )

printf "$V"
bash: printf: `&': invalid format character
  • @terdon In case you didn't follow this from the beginning, the OP was deleting his questions after receiving answers. But, if you need a bad guy to point to and you picked me, sure, I'm here, ready and willing. shrug – Satō Katsura Feb 22 '17 at 12:40
  • @SatoKatsura I know, I have been following closely. And I wasn't saying you did anything wrong, sorry if it came across that way. The only "bad guy" here is the user who deletes useful questions, certainly not you! – terdon Feb 22 '17 at 12:51
5

The first argument to printf will be interpreted as a formatting string, and %& is an invalid format specifier.

Try this instead:

printf '%s' "$V"

The formatting that printf does should be explained in the printf(1) manual, or in the manual of your shell. The %s format simply means "expect a string". The printf utility does not add a newline by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy