I'm having trouble matching a wilcarded string in the sh shell. The code is part of a configure.ac so it usually gets the lamest shell available. I can't count on the Bash shell.

The code first executes:

LDNAME=`basename "$LD"`

And I sometimes get a string like ld -m x86_64 or ld -m elf64lppc, but I want just ld as a friendly display name.

I then perform a small fixup according to How to check substring in shell script bash?, Substring check in IF condition in shell script and if/else comparison with wildcard using a case statement:

case "$LDNAME" in
    "ld -m*") LDNAME=ld

The string fails to match and I get the original string back. I've also tried ld -m * (without quotes) and a few other variations with the same result.

How do I match a wilcarded string in case statement using sh shell?

This works but it is not very extensible:

if test "$LDNAME" = "ld -m elf_x86_64"; then
if test "$LDNAME" = "ld -m elf64lppc"; then

1 Answer 1


This should work:

LDNAME="ld -m blah"
case "$LDNAME" in "ld -m"*) echo foo;; esac

You need to leave the wildcard * unquoted, otherwise it's taken as a literal character.

But really, if all you want is to remove the -m and anything after it, you could just use LDNAME=${LDNAME%% -m*}.

LDNAME="ld -m blah"
echo ":$LDNAME:"

gives :ld:.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy