31

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

Captain Man
  • 1,186

2 Answers2

55

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

s/\/some\/path\/here/\/other\/path\/now/

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:

s#/some/path/here#/other/path/now#

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
Kusalananda
  • 333,661
18

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.

manifestor
  • 2,473
  • 2
    Backslashes only work in some implementations of sed, e.g. GNU sed. – Kusalananda May 11 '18 at 08: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 08:42
  • @fedorqui Why should they not be used? It's working in sed 4.4, so you can use it. – manifestor May 11 '18 at 08:45
  • 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 08:52
  • yes, but he did not demand that the answer should be POSIX compliant. – manifestor May 11 '18 at 08:55
  • 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 09: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
  • 1
    @hvd how about sed -e 'ss ssg' (or similarly, sed -e 'sg ggg') XD – Doktor J May 11 '18 at 14:12
  • Note that converting multiple spaces to just one can be done in an easier manner with tr -s ' ', which stands for squeezes. – fedorqui May 16 '18 at 10:15