I want to detect if the input argument contains some sub-string.
I tried below scripts:
#1:
#!/bin/sh
if [ $1 = abc ]
then
echo contains abc
else
echo not contains abc
fi
#2:
if [ "$1" = "*abc*" ]
then
echo contains abc
else
echo not contains abc
fi
#3:
if [ "$1" = *"abc"* ]
then
echo contains abc
else
echo not contains abc
fi
All #1 ~ #3 don't work.
But below ones with [[
can easily work:
#4:
if [[ $1 = *"abc"* ]]
then
echo contains abc
else
echo not contains abc
fi
#5:
if [[ $1 = *abc* ]]
then
echo contains abc
else
echo not contains abc
fi
So is it possible to make the single [
work?
ADD 1 - 10:55 AM 9/21/2021
Just found a VERY USEFUL thread:
What is the difference between the Bash operators [[ vs [ vs ( vs ((?
[
work with the pattern. Counter-question: is there a reason to want to use[
in particular, when Bash supports[[
which does exactly what you want? – ilkkachu Sep 21 '21 at 07:35