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.

1 Answer 1

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.

2
  • it was shown near the end of the question that is works for proc w/out quotes for me on my PC. Commented Dec 23, 2021 at 2: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
    Commented Dec 23, 2021 at 2:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .