Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parameter expansion pattern replacement does not match empty string at beginning/end #29

Closed
dicktyr opened this issue Jan 31, 2017 · 6 comments
Labels

Comments

@dicktyr
Copy link

dicktyr commented Jan 31, 2017

ksh ignores empty string pattern and does not perform substitution

% ksh -c 'x=a; echo ${x/#/.}'
a

by contrast bash matches and substitutes as expected

% bash -c 'x=a; echo ${x/#/.}'
.a

% ksh --version
  version         sh (AT&T Research) 93u+ 2012-08-01
@dannyweldon
Copy link

Agreed, and zsh behaves the same as bash as well.

@McDutchie
Copy link
Contributor

McDutchie commented Oct 6, 2017

Not sure if this is a bug or a legitimate implementation difference. Nothing in the ksh93 man page says that the pattern following # or % may be empty.

yash does not recognise an empty pattern there either.

@siteshwar
Copy link
Contributor

It's just a difference between how shells behave differently on empty patterns like '#' and '%'. POSIX does not define how ${var/pattern/replacement} should work. I think we should close this issue as not a bug.

@McDutchie
Copy link
Contributor

Yes. It could be a feature request instead of a bug. This does seems like a rather convoluted way of adding something to the beginning or end of a variable. You can simply string them together like echo .${a}. But scripters do strange things all the time, and better compatibilty with bash and zsh could be considered a reason to change this.

@kernigh
Copy link
Contributor

kernigh commented Apr 21, 2018

bash and zsh don't agree about empty patterns. Given try.sh with

x=abcd
echo ${x/''/e}
echo ${x//''/e}

then

$ bash try.sh
abcd
abcd
$ zsh try.sh
eabcd
eaebeced

Here ksh acts like bash.

@McDutchie
Copy link
Contributor

But the pattern reported was #, not empty.

$ cat try.sh
x=abcd
echo ${x/#/e}
echo ${x//#/e}
$ bash try.sh
eabcd
abcd
$ zsh try.sh
eabcd
eabcd

Meh. I'm with @siteshwar ... close as not a bug.

@siteshwar siteshwar added notabug and removed bug labels Jun 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants