Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Can I test if a parameter expansion has worked



2022-09-15 08:34:34 +0100, zzapper:
[...]
> f=stuff.jpg;
> 
> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
> 
> stuff_s.jpg  # good this is what i want
> 
> # now pass 'bad' input
> 
> f=stuff.txt;
> 
> print ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH}
> stuff.txt  #  expansion has obviously left the input unchanged but can I
> test for that???
[...]

You could always do:

unset flag
print -r -- ${f/%(#m).[pjg][npi]e#[gf]/_s$MATCH${flag=}}
if (( $+flag )) echo it did substitute

Note that "print $var" without the -- is a command injection
vulnerability and without -r generally not what you want.

-- 
Stephane




Messages sorted by: Reverse Date, Date, Thread, Author