15

A simple question, but I can't find the answer. the zsh documentation overwhelms me :-(

function z ()
{
  echo ${1:s/the/THE}
}

$ z thethethe
THEthethe

...

How can I get all 'the' to be replaced with 'THE', not just the first occurrence? With zsh it's the easiest things that seem to be the hardest to find examples for.

25

This is a history expansion modifier, so add the g modifier immediately before s.

echo ${1:gs/the/THE}

Alternatively, use the parameter expansion syntax where a double slash means a global substitution. In this case, the part after s// is a wildcard pattern, whereas history expansion substitution takes a literal string.

echo ${1//the/THE}
8

This should do the job:

VARIABLE=thethethe
echo ${VARIABLE//the/THE}
THETHETHE
4

Thereyago, it had to be simple:

echo ${1//the/THE}

... the double slash does it, and you can't fool around with " :s "

BTW, I'd sure like to thank the 'StackExchange' sites for existing. Of all the zillions of places one might ask a question, these are by far the best--one not only almost always gets answers, but those answers are invariably of the highest quality.

1

// works perfect if you're only trying to replace a specific word to all caps while leaving the rest of it untouched. If you are trying to convert everything to uppercase, you can use ":u" on the variable

SOME_ENV_VALUE='thethethe'
echo $SOME_ENV_VALUE:u // outputs: THETHETHE

You can verify which modifiers you have available in the zsh expansion and substitution manual

man zshexpn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.