I wrote the following command in order to match $a with $b, but when the value includes "-", then I get an error. How can I avoid that?
# a="-Xmx5324m" # b="-Xmx5324m" # # # echo "$a" | grep -Fxc "$b" grep: conflicting matchers specified
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
Besides of @sebasth's great answer, you could tell that PATTERN with
grep's -e option to use PATTERN as a pattern (here using the
zsh here-string operator instead of
echo; see also
printf '%s\n' "$a" for portability).
grep -Fxc -e "$b" <<<"$a"
Or all beside of other options.
grep -Fxce "$b" <<<"$a"
Since your goal is byte-to-byte string equality comparison use the
[ command instead.
if [ "$a" = "$b" ]
$b, using the
[[...]] ksh construct:
if [[ $a == *"$b"* ]]
Or more portably in all Bourne-like shells:
case $a in *"$b"*) ... esac