sd icon indicating copy to clipboard operation
sd copied to clipboard

sd returns exit code 0, even when there's no match

Open iiiraq opened this issue 2 years ago • 6 comments

When sd doesn't match an input, it should not return with exit code 0 this will cause issues when scripting.

echo 'target' | sd 'miss' 'hit' && echo $? target 0

iiiraq avatar May 23 '22 05:05 iiiraq

Sorry, but I do not think it is a bug for me...

$ echo 'target' | sed 's/miss/hit/' && echo $?
target
0

sed returns 0 as well because it does finish its task well.

PeterlitsZo avatar Jun 04 '23 12:06 PeterlitsZo

People may want to easily know if nothing matched the pattern. At the very least it would be nice to add a flag that fails with a non-zero status if there was no match

CosmicHorrorDev avatar Jun 04 '23 15:06 CosmicHorrorDev

More information: https://stackoverflow.com/a/15965681/13031497

Edit: Sorry about that I misunderstand. I also agree with a new flag.

PeterlitsZo avatar Jun 13 '23 15:06 PeterlitsZo

I'm not sure where there's a disagreement. What you said matches my

At the very least it would be nice to add a flag that fails with a non-zero status if there was no match

I was thinking something along the lines of a --must-match flag

Edit: Just to be clear I wasn't thinking of changing the default behavior. We should typically default to matching sed's behavior which we do already here

CosmicHorrorDev avatar Jun 13 '23 16:06 CosmicHorrorDev

Mark this as a feature request then, not a bug

dev-ardi avatar Oct 20 '23 02:10 dev-ardi

I would like to second this. Just ran into an issue like this, and had to use oldschool sed instead of sd in my script because of how troublesome it is to detect whether any matches actually occurred.

crabdancing avatar Feb 17 '24 20:02 crabdancing