I need to compare strings in shell:
var1='mtu eth0' if [ '$var1' == 'mtu *' ] then # do something fi
But obviously the ‘*’ doesn’t work in Shell. Is there a way to do it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
bashShortest fix:
Bash’s
[[ ]]doesn’t get glob-expanded, unlike[ ](which must, for historical reasons).bash --posixOh, I posted too fast. Bourne shell, not Bash…
${var1:0:4}means the first four characters of$var1./bin/shAh, sorry. Bash’s POSIX emulation doesn’t go far enough; a true original Bourne shell doesn’t have
${var1:0:4}. You’ll need something like mstrobl’s solution.