Shell/bash script get line number on match

There's a part of my script where i need to find line number of first match in file, i use:

local lineNum=$(grep -m 1 -n "$search" "$filePath" | head -1 | cut -d : -f1)

Problem is that if $search is something like Name[cs_CZ]=, then it fails and find nothing, most likely it's some parsing trickery about [ ], everything else is succesfull on getting lineNum...

Any ideas? :confused:

More info. What line are you working on? Maybe the full script and data would help. At lease the line# and expected result.

is at user to "escape" $search with \ char : name\[xxxx\] or you write your escape function

search="$(printf "%q" "$search")"
2 Likes

Thx man, escape function worked like charm :slightly_smiling_face:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

Forum kindly sponsored by