Nicolas Berthier
Nicolas Berthier

Reputation: 508

Double quotation mark escaping in alias

I am trying to set the following alias in Debian Stretch

alias myalias='watch -d -n 0.1 '\''find /path -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c'\'''

I have tried to escape the first set of quotation marks with '\'' but it doesn't work with the deeper marks around

%TY-%Tm-%Td\n

When I run the command, I end up with the following output. The quotation marks around the %TY-%Tm-%Td\n being not there anymore, the output does not interpret the \n and the result is on one line.

Every 0.1s: find /root/bolero/bolero/pkl/stocks -type f -printf %TY-%Tm-%Td\n | sort | uniq -c

Any idea to make this work?

Upvotes: 0

Views: 74

Answers (1)

bk2204
bk2204

Reputation: 76519

The output you're looking for is this:

alias myalias='watch -d -n 0.1 '\''find /path -type f -printf '\''\'\'''\''%TY-%Tm-%Td\n'\''\'\'''\'' | sort | uniq -c'\'''

This is, of course, enormously complicated.

Because nobody wants to count quotation marks, let me introduce you to a feature of Git you may not have known about: git rev-parse --sq-quote. If you want to know how text would be properly single-quoted, instead double-quote that portion and pass it to git rev-parse --sq-quote. So incrementally, it looks like this:

$ git rev-parse --sq-quote "find /path -type f -printf '%TY-%Tm-%Td\n' | sort | uniq -c"
 'find /path -type f -printf '\''%TY-%Tm-%Td\n'\'' | sort | uniq -c'
$ git rev-parse --sq-quote "watch -d -n 0.1 'find /path -type f -printf '\''%TY-%Tm-%Td\n'\'' | sort | uniq -c'"
 'watch -d -n 0.1 '\''find /path -type f -printf '\''\'\'''\''%TY-%Tm-%Td\n'\''\'\'''\'' | sort | uniq -c'\'''

And that's how you get your result. Note that Git will insert a leading space on the line, which you may want to remove for tidiness.

Upvotes: 3

Related Questions