1

In user's console I have bash:

$ echo $SHELL
/bin/bash
$ bash --version
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)

I have code in file test.sh:

$ cat test.sh
aaa='---aa-aa---'
echo "${aaa}"
echo 'does not work...'
# trim "-"
echo ${aaa/+(-)}
echo ${aaa%%+(-)}
echo 'works for one symbol...'
echo ${aaa%-}
echo ${aaa/-}

The last two rows work fine but previous ones.

$ bash test.sh
---aa-aa---
does not work...
---aa-aa---
---aa-aa---
works for one symbol...
---aa-aa--
--aa-aa---

In the same time, if you would try to make this console it works:

$ aaa='---aa-aa---'
$ echo ${aaa/+(-)}
aa-aa---
$ echo ${aaa%%+(-)}
---aa-aa

So, why it doesn't work in a script?

2
  • Trying to decide whether to close this as a duplicate of stackoverflow.com/questions/26726264/…, or do it the other way 'round. @tripleee, do you have an opinion here? Commented Oct 21, 2016 at 16:04
  • ...actually, on review, I think this instance of the question is more clearly asked than the other, enough to negate the other's temporal advantage (from being asked first).. Commented Oct 21, 2016 at 16:06

1 Answer 1

5

You seem to have shopt -s extglob enabled in your interactive shell, which turns on extended globbing. This is not the default behavior, and needs to be explicitly enabled in your script. See extended pattern matching in the bash hackers wiki for details.

Sign up to request clarification or add additional context in comments.

1 Comment

Without shopt -s extglob, the pattern +(-) is a literal 4-character string to match, not a pattern matching one or more -.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.