Skip to content
The Bash Insectarium

/static/insetario/en/inseto/param-troca/

Menu

BSH-011 · Parameters

${var/velho/novo}

Swaps one piece of the value for another; with a double slash, all of them.

A pocket sed inside the expansion: ${var/a/b} swaps the first occurrence of the pattern, ${var//a/b} swaps them all. The pattern takes glob wildcards.

Anchors come for free: ${var/#a/b} only matches at the start, ${var/%a/b} only at the end. Without the new piece — ${var/a} — the swap becomes removal.

$ trilha="formiga formiga cigarra"
$ echo ${trilha/formiga/abelha}
abelha formiga cigarra
$ echo ${trilha//formiga/abelha}
abelha abelha cigarra
One slash swaps the first; two swap the whole swarm.