Why double "\" required?

Hi Gurus,

Below command give error if i put single "\", If I put two, it works. my understanding is: we can use "\" to escapt the special characters. why here I need to put two "\". The system I am working on is SunOS 5.10 Generic_150400-04 sun4v sparc sun4v

echo "SUCCESS(ABCD)" |nawk '{gsub("SUCCESS\\(", "")}1'
ABCD)

echo "SUCCESS(ABCD)" |nawk '{gsub("SUCCESS\(", "")}1'
nawk: illegal primary in regular expression SUCCESS( at 
 source line number 1
 context is
        {gsub("SUCCESS\(", >>>  "") <<<

It is because awk substitutes \ in strings. For example

nawk 'BEGIN {print "\a"}' </dev/null
nawk 'BEGIN {print "\b"}' </dev/null

So within " " you need \\ for a literal \
Therefore, you better have regular expressions in / /

echo "SUCCESS(ABCD)" | nawk '{gsub(/SUCCESS\(/, "")}1'
1 Like

Hello green_k,

\\( means escape \ to remove its special meaning. If you will put ( without \ will be treated as sub's starting ( parenthesis and it will give error.

Thanks,
R. Singh

1 Like