sedmetacharacters

sed - how to replace meta character


In the following example I am looking to include the * metacharacter in the string replacement. If I understand correctly, I should be able to escape the character using \ but after doing that this is the result I am seeing:

❯ echo 'foo(*)' | sed s/foo\(\*/bar/g
bar*)

I was expecting to see the following:

❯ echo 'foo(*)' | sed s/foo\(\*/bar/g
bar)

Solution

  • You need to quote your sed command:

    echo 'foo(*)' | sed 's/foo(\*/bar/g'
    bar)