0

From https://unix.stackexchange.com/a/378549/674

tmp=${string//"$separator"/$'\2'} 

What does $ in $'\2' mean?

Is $'\2' a parameter expansion?

Thanks.

0

1 Answer 1

3

This is ANSI-C Quoting:

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

$'\2' is expanded to the eight-bit character whose value is the octal value 2. In the answer you refer to, this character is used as a field separator.

4
  • Thanks. In C language, how is $\2 specified instead? Commented Aug 1, 2017 at 14:24
  • $ introduces the string in Bash, it has no special meaning in C; if you wanted to store the string $\2 in C, you’d just escape the backslash, $\\2. Commented Aug 1, 2017 at 14:28
  • Thanks. In C, how is the octal value 2 specified? Is it 0x2? Is there a similar metacharacter in C to $ in Bash? Commented Aug 1, 2017 at 14:30
  • In C, in a numeric context you just use a leading 0 for an octal value, so 010 is 8; in a string context with a backslash, values are treated as octal anyway. There’s no need for a $-style metacharacter, backslashes are interpreted in this way in all C strings: you’d just write "\2". Commented Aug 1, 2017 at 14:42

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.