This question already has an answer here:

I found line sed 's~ ~~g' in a shell script on a Linux system. What is this ~?

marked as duplicate by muru, don_crissti, Jeff Schaller, Rui F Ribeiro, hildred May 20 '18 at 9:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 6
    In this case tilda is delimiter. Did you read sed man page? – Romeo Ninov May 11 '18 at 7:54
  • 24
    @RomeoNinov s~tilda~tilde~ – Konrad Rudolph May 11 '18 at 10:36
  • 7
    Not sure this qualifies for closure under "requests for learning materials" as the question is very simply & directly: what is this ~ doing here in this sed script? – Jeff Schaller May 11 '18 at 10:58
  • 1
    @RomeoNinov - a rhetorical question, I suppose... the obvious answer is No, they did not. Plenty of people who didn't read it, judging by the number of upvotes here... – don_crissti May 11 '18 at 13:35
  • 1
    @muru - yeah... there's a question like this for every possible character (e.g. sed exclamation marks etc) yet almost no one bothers searching... U&L is slowly turning into a new SO... – don_crissti May 14 '18 at 13:03

It's an alternative delimiter for the sed substitute (s) command. Usually, the slash is used, as in s/pattern/replacement/, but sed allows for almost any character to be used.

The main reason for using another delimiter than / in the sed substitution expression is when the expression will act on literal / characters.

For example, to substitute the path /some/path/here with /other/path/now, one may do


This suffers from what's usually referred to as "leaning toothpick syndrome", which means it's hard to read and to properly maintain.

Instead, we are allowed to use another expression delimiter:


Using ~ is just another example of a valid substitution expression delimiter.

Your expression

s~ ~~g

is the same as

s/ //g

and will remove all spaces from the input. In this case, using another delimiter than / is not needed at all since neither pattern nor replacement contains /.

Another way of doing the same thing is

tr -d ' ' <infile >outfile

It's a substitution delimiter. The most often used delimiter is a forward slash /. But sed can use any character as a delimiter - it will automatically use the character following the s as a delimiter:

sed 's@ @@g'
sed 's+ ++g'
sed 's\ \\g'
sed 's* **g'

Also it is possible to use space as delimiter:

echo "foo" | sed 's foo bar g'

Other delimiters than / are most often used when you have many slashes in your command in the syntax is getting confusing. Consider this case where we want to convert multiple spaces to just one space character: Instead of

echo "    a   b  c  " | sed 's/\s\{1,\}/ /g'

you can run

echo "    a   b  c  " | sed 's~\s\{1,\}~ ~g'

which is more clear.

  • 2
    Backslashes only work in some implementations of sed, e.g. GNU sed. – Kusalananda May 11 '18 at 8:07
  • 1
    Any character other than backslash or newline can be used instead of a slash to delimit the BRE and the replacement (reference) – fedorqui May 11 '18 at 8:42
  • 2
    This is what POSIX says, it is not me :) Having it run in some implementations (like @Kusalananda mentions) is fine, only that you have to be aware that it won't be the case in all of them – fedorqui May 11 '18 at 8:52
  • 1
    Of course :) He did not demand any explanation on extra delimiters, but since you kindly explained that, showing the whole picture will be even more useful! – fedorqui May 11 '18 at 9:00
  • 2
    It's even possible to use the space itself as the delimiter, so long as it's escaped in the pattern: sed -e 's \   g'. (Don't copy & paste that: I've had to use non-breaking spaces to prevent the site from messing it up.) But don't ever write that in code that's meant to remain readable even a minute later. – hvd May 11 '18 at 10:25

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