I have several bash scripts which read arbitrary strings (usually from stdin). When those strings contain things that bash recognizes, then things break.
For example, I have a simple logging script (named al) whose main function is to take an input string and write it to a file with a timestamp in front of it.
If I give it things like
al that didn't work
or
al I tried a & B
it breaks because bash sees the quote or ampersand (or other metacharacter) and tries to process it before it even gets to my script.
I would like to have a way to accept the string and run it through a filter to get rid of or add escapes ("\") to all the characters that may cause problems before I pass the string to the rest of my script.
I wrote a small C program to do it - letting it read the raw input, so bash never sees it. It works, but I don't really want to reinvent the wheel.
I know I can just input the strings manually with the characters escaped, but I usually forget to do that and sometimes they get stripped later anyway.
al I tried a \& b
al It didn\'t work
Is there some utility or a way to do this without writing it from scratch?