0

I'm surprised that:

$ if [ -n "$(findmnt | grep "\""proc"\"" | head -n 1)" ]; then echo 1; else echo 2; fi
2

I've used "\"" before IIRC after reading https://unix.stackexchange.com/a/187452/446998, after all it works:

$ echo "1"\""2"\""3"
1"2"3

Why it doesn't in a first case?

Revisiting How to escape quotes in shell? and trying to follow accepted answer resulted in opposite issue: finding non-existing mounts:

$ if [ -n $'$(findmnt | grep "\proc111"\ | head -n 1)' ]; then echo 1; else echo 2; fi
1

It works w/out quotes for grep inside:

~$ if [ -n "$(findmnt | grep proc111 | head -n 1)" ]; then echo 1; else echo 2; fi
2
~$ if [ -n "$(findmnt | grep proc | head -n 1)" ]; then echo 1; else echo 2; fi
1

I've tried to read suggested Escaping double quotes inside command substitution but could not immediately apply to my code.

Martian2020
  • 1,167

1 Answers1

1

The problem does not seem to be that escaping double quotes is incorrect, but that there is no "proc" (enclosed in double quotes) being matched from the output of findmnt.

The default findmnt output only shows /proc for TARGET and proc for SOURCE and FSTYPE (at least in all the findmnt versions I've seen).

findmnt -J does output "proc" and so that would match with your expression, but then again it would make more sense to use a JSON parser like jq.

Snake
  • 1,650
  • it was shown near the end of the question that is works for proc w/out quotes for me on my PC. – Martian2020 Dec 23 '21 at 02:05
  • That's expected since grep is not searching for literal double quotes in the pattern, which does not exist in the default output of findmnt. – Snake Dec 23 '21 at 02:35