help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-bash] bash variables and regex comparison, [[ ]] builtin


From: Lunda Jaroslav
Subject: [Help-bash] bash variables and regex comparison, [[ ]] builtin
Date: Fri, 22 Jan 2016 10:30:28 +0100 (CET)

   Let x='abc.xyz' and y='abc:xyz' so that the following holds true
   (prints "matches" and "diff"): [[ "${x}" =~ abc".xyz" ]] && echo
   "matches" [[ "${y}" =~ abc".xyz" ]] || echo "diff" Now, literal
   l=".xyz" can be extracted and tests still work (note double quotes
   around l refs): [[ "${x}" =~ abc"${l}" ]] && echo "matches" [[ "${y}"
   =~ abc"${l}" ]] || echo "diff" And the problem: if we try further
   r="abc\"${l}\"" or r="abc${l}", the first test never prints "matches":
   [[ "${x}" =~ ${r} ]] && echo "matches" [[ "${y}" =~ ${r} ]] || echo
   "diff" What should be the proper form of r to pass both tests? Please
   note: I'm aware of solutions like quoting all possible metachars in l
   but I'm curious how variable expansions works in context of the [[ ]]
   builtin. So preffered solution should use quoting to treat ".xzy" as
   literal sequence, not produce something like "\.xyz". Posted also here:
   http://stackoverflow.com/questions/34930099/bash-variables-and-regex-co
   mparison


reply via email to

[Prev in Thread] Current Thread [Next in Thread]