View Full Version : [SOLVED] How do You Compare Two Strings With Wildcards in Bash?
April 6th, 2009, 07:45 PM
if [ "foobar" == *"bar" ]; then # Won't work because you need double brackets '[[...]]'
if [[ "foobar" == "*bar" ]]; then # Will not work because * is inside quotes
if [[ "foobar" == *"bar" ]]; then # Will work
Thanks to everyone who helped!
April 6th, 2009, 07:51 PM
I am not sure if this is what you're looking for but if I were you I would set a variable to take the input or if you want
'$*' will give you the entire input argument.
April 6th, 2009, 08:11 PM
It has to do with quotation, see this link (http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_03.html)
$ if [[ "abc: GPG encrypted data" = *': GPG encrypted data' ]]; then echo Match
April 6th, 2009, 08:44 PM
Thank you! I just needed to put the '*' outside the quotation marks and use double square-brackets.
April 6th, 2009, 08:45 PM
Here is the correct code:
# Compare Test
if [[ "abc: GPG encrypted data" = *": GPG encrypted data" ]]; then
else echo "No match."
April 7th, 2009, 01:39 AM
you can also use case/esac
April 7th, 2009, 11:17 AM
Unless I am actually comparing multiple items, I think I'll stick with if for now, thanks anyawy!
Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.