When doing server maintenance, I often find myself writing multi-level nested commands, so as:
for server in A B C; do sh -c "ssh $server 'mysql db -e \"select where line like \\\"%abc%\\\"\"'&"; done
This may be a contrived example, but it's really the kind of thing I have to do sometimes for some odd, ad-hoc task.
The problem is the quotes. As you go to deeper levels, you have to start escaping the quotes, escaping dollar signs, and escaping the escaping. Using single quotes helps avoid one level, but that's it.
I began working on a solution for myself, inspired by the qq() function in perl, but I thought I'd ask around first: is there a tool, method or framework that will make it easier to write these nested commands without getting bogged down with backslashes?
You can avoid most layers of quoting by passing the mysql commands via stdin instead of via argv:
For a more generic answer, see http://mywiki.wooledge.org/BashFAQ/096
Try this:
It decreases the nesting level, and also makes it much easier for you because you can just run the echo command until you get the exact command you want run after all escaping.