# search & replace in variable

**URL:** <https://community.unix.com/t/search-replace-in-variable/156289>\
**Category:** Shell Programming and Scripting\
**Created:** [September 27, 2005, 3:14am UTC](https://community.unix.com/t/search-replace-in-variable/156289 "2005-09-27T03:14:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![malaymaru](https://community.unix.com/letter_avatar/malaymaru/32/6_449c533f586939b6e5f246632a46421f.png) [@malaymaru](https://community.unix.com/u/malaymaru)\
**Post date:** [September 27, 2005, 3:14am UTC](https://community.unix.com/t/search-replace-in-variable/156289/1 "2005-09-27T03:14:46Z")

</div>

Can I use search & replace in any variable?

Suppose I have one variable named var1 which holds value "abcabc" I need to search 'a' in var1 and want to replace with 'x' like 'xbcxbc'. Is it possible? Can you provide me an example?

Malay

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [September 27, 2005, 3:20am UTC](https://community.unix.com/t/search-replace-in-variable/156289/2 "2005-09-27T03:20:58Z")

</div>

If you are using sh read the man pages of sh

Under the Parameter Expansion section

```plaintext
       ${parameter/pattern/string}
       ${parameter//pattern/string}
              The pattern is expanded to produce a pattern just as in pathname
              expansion. Parameter is expanded and the longest match of pat-
              tern against its value is replaced with string. In the first
              form, only the first match is replaced. The second form causes
              all matches of pattern to be replaced with string. If pattern
              begins with #, it must match at the beginning of the expanded
              value of parameter. If pattern begins with %, it must match at
              the end of the expanded value of parameter. If string is null,
              matches of pattern are deleted and the / following pattern may
              be omitted. If parameter is @ or *, the substitution operation
              is applied to each positional parameter in turn, and the expan-
              sion is the resultant list. If parameter is an array variable
              subscripted with @ or *, the substitution operation is applied
              to each member of the array in turn, and the expansion is the
              resultant list.

```

That would translate to a construct like this

```plaintext
${var1//a/x}

```

I couldnt find any similiar shell builtin for ksh.

There are a bunch of solutions for something similiar in this thread [Find and replace the value in a variable](http://www.unix.com/showthread.php?t=21655)

vino

---

<div class="post-metadata">

**Author:** ![monsanbu](https://community.unix.com/letter_avatar/monsanbu/32/6_449c533f586939b6e5f246632a46421f.png) [@monsanbu](https://community.unix.com/u/monsanbu)\
**Post date:** [October 4, 2005, 5:18am UTC](https://community.unix.com/t/search-replace-in-variable/156289/3 "2005-10-04T05:18:06Z")

</div>

Why can't you echo the variable to sed and search and replace

echo "abcabc" | sed 's/a/x/g'

---

<div class="post-metadata">

**Author:** ![vino](https://community.unix.com/user_avatar/community.unix.com/vino/32/156_2.png) [@vino](https://community.unix.com/u/vino)\
**Post date:** [October 4, 2005, 5:36am UTC](https://community.unix.com/t/search-replace-in-variable/156289/4 "2005-10-04T05:36:19Z")

</div>

> [@monsanbu](#):
>
> Why can't you echo the variable to sed and search and replace
> 
> echo "abcabc" | sed 's/a/x/g'

There is nothing wrong in doing that.

But why invoke an external program (sed, in this case) when the shell allows you to do the same thing.
