-1

I was looking at this link Check if a Bash array contains a value which says how to check for existence of an item in a list as follows:

if printf '%s\n' ${myarray[@]} | grep -q -P '^mypattern$'; then
    # ...
fi

However, I want mypattern value to be passed as a variable as follows:

mynewpattern="xyz"

then I was expecting the following to work

if printf '%s\n' ${myarray[@]} | grep -q -P '^"$mynewpattern"$'; then
    # ...
fi

But it is not picking the new pattern of xyz. What is the appropriate syntax to insert the new pattern?

I have just started learning bash.

user3243499
  • 2,411
  • 5
  • 23
  • 44
  • 1
    Use: `grep -q -P "^$mynewpattern$"` that is using double quotes only. – anubhava Nov 27 '19 at 18:25
  • @anubhava nope, it is not working. My confusion without double quotes is how will bash differentiate between `$` of my variable and `$` of the regex? – user3243499 Nov 27 '19 at 18:29
  • It does work, your pattern may be incorrect. You've got 2 similar answers below. and `$` without anything afterwards is not expanded as variable. – anubhava Nov 27 '19 at 18:32

2 Answers2

1

The single quotes are wrong; you want double quotes instead of single.

However, grep -P is also slightly wrong here; it's not properly portable, and your pattern doesn't use any of the syntax which -P enables; also, you should quote your array properly.

if printf '%s\n' "${myarray[@]}" |
    grep -q "^$mypattern\$"
then
    ...

Text between single quotes is passed through verbatim. If you want the shell to perform variable interpolation, use double quotes (and then you need to escape any literal backslash, dollar sign, or backtick).

tripleee
  • 139,311
  • 24
  • 207
  • 268
  • Thanks it worked and also thank you very much for the explanation. Since, I have just started learning bash and you seem to have mastered bash, could you please recommend me a few resources which I can read more about bash programming? – user3243499 Nov 27 '19 at 18:36
  • 1
    There's a brief link collection near the end of tae [Stack Overflow `bash` tag info page.](/tags/bash/info) – tripleee Nov 27 '19 at 18:37
  • 1
    There is no need to escape last `$` however it will work with escaping as well. – anubhava Nov 27 '19 at 18:38
  • Yeah, I cut a few corners there. The circumstances under which dollar signs *must* be escaped are somewhat more complex but I felt this was adequate for a beginner's understanding. – tripleee Nov 27 '19 at 18:39
  • 1
    @anubhava, yes you are right sir, it works with escaping not sure why for OP it is not working. I just tested on repl site and worked(mentioned in mine example) too, cheers. – RavinderSingh13 Nov 27 '19 at 18:42
0

Could you please try using like grep -q -P "^$var$"(in your script) Here is an example script for same scenario for an Input_file(since no samples for array elements were provided so explaining it with an sample/example script here).

##Shell variable
var="bla"

##A sample Input_file
cat << EOF > Input_file
blabla test test
123abcd123test
bla
EOF

##Following is the code to check.
if grep -q -P "^$var$" Input_file
then
     echo "match found."
fi

Above will only match lines which are starting with variable val's value.

RavinderSingh13
  • 101,958
  • 9
  • 41
  • 77