1

Here is my script:

#!/bin/bash

x="aabcaa"
y=${x##*(a)}
echo $y

It outputs aabcaa when I expect bcaa.

When I try the same commands on a bash shell (got with bash command from zsh shell), everything works as I want. I checked the version with bash --version and /bin/bash --version. Obviously, the two commands output the same thing. The unexpected behavior of the script is the same in a zsh shell and in a bash shell.

What could be wrong?

David Medinets
  • 5,160
  • 3
  • 29
  • 42
paulvand
  • 31
  • 1
  • 5
    You probably have the `extglob` option (enabling extended glob pattern syntax) set in your interactive shell (set in ~/.bash_profile or somewhere like that). If you want to use extended glob syntax in a script, you need to add `shopt -s extglob` at the beginning of the script. – Gordon Davisson Aug 30 '20 at 12:47
  • Run this: `shopt -p >inter; bash -c 'shopt -p >noninter'; diff --suppress-common-lines -y inter noninter` or this: `{ shopt -p; bash -c 'shopt -p';} | sort -k3 | uniq -u` – Léa Gris Aug 30 '20 at 14:09
  • Sorry Gorgon, I thought I had thanked you and I hadn't. You solved my problem (without any delay !). – paulvand Sep 13 '20 at 21:26

0 Answers0