Score:1

Can't escape a dot using backslash, Why?

in flag

So, following are the commands are I ran:

$ ~/Documents: touch ball.txt bool-txt bowl.txt bull.txt
$ ~/Documents: ls . | grep b..l\.txt
ball.txt
bool-txt
bowl.txt
bull.txt

I wasn't expecting bool-txt to be present in the output, because I have escaped the dot but its still not considering it a literal dot. Where am I going wrong?

qwr avatar
kr flag
qwr
that might be shell escaping, try putting it in a string
qwr avatar
kr flag
qwr
also in general parsing ls is error prone, but probably not relevant to this question
Som Shekhar Mukherjee avatar
in flag
Yeah this `ls . | grep b..l\\.txt` works and also this `ls . | b..l"\.".txt`. Is this what you meant? Could you explain mean what is happening here or point me to relevant resources. @qwr
Score:5
kr flag
qwr

I'm sure this is a duplicate of some question but I can't find it. On Unix SE https://unix.stackexchange.com/questions/583428/why-do-i-have-to-quote-an-escaped-character-in-a-regular-expression-for-grep-bu

When you use grep b..l\.txt, the shell converts \. into . so grep receives b..l.txt, then it interprets . as any character.

To prevent this, the easiest method is giving grep a string like grep 'b..l\.txt', then the shell won't escape the dot. Note that single quotes and double quotes do different things in bash but that doesn't matter in this case. Alternatively, use double backslash so the shell escapes the backslash as a literal character and grep correctly receives \..

Som Shekhar Mukherjee avatar
in flag
Also could explain this "also in general parsing ls is error prone"? It would be really helpful.
qwr avatar
kr flag
qwr
long story https://unix.stackexchange.com/q/128985/171025
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.