Sed Using Declared Variable


Here is a small tip to use declared variables with sed command, since there is difference between using variable in other shell commands and sed I have been written this post.

In most of shell commands we use declared variables with $varaiable_name but with you need to use it as ${variable_name}


First declare a variable as

$ var1=a
$ var2=st

Now use the variable var1 and var2 in sed expression as shown below

$ echo "abc" | sed "s/${var1}/${var2}/g"

The above expression will replace all var1 with var2 and the output we get after running the above command is


One comment on “Sed Using Declared Variable

  1. Pingback: Useing bash variable with awk | Sany's Linux and Open Source Blog

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s