Skip to main content

What does `[$'\r\n']` mean? [Resolved]

I regularly use the expression line=${line//[$'\r\n']}. But what does [$'\r\n'] mean?

I know it removes the '\r\n' characters but how does it do this? Does it remove the instances of both characters only, or does it also find matches of just one character?

I do not understand the usage of this syntax.

If you can, please, give me a link to the manual. I cannot find the answer on this question.

Question Credit: UnixUser
Question Reference
Asked July 13, 2019
Posted Under: Unix Linux
3 Answers

This command removes all carriage return and line feed bytes from the line variable.

This is a Bash (and zsh, and ksh) extension to parameter expansion: ${line//XYZ} replaces all matches of the pattern XYZ in $line with nothing.

The pattern [$'\r\n'] has two components: [...] matches any of the characters inside it. $'\r\n' is ANSI-C quoting (another extension), and expands to the carriage return (\r) and line feed (\n) bytes, so [$'\r\n'] is [...] with a carriage return and line feed inside it, and so matches both of them.

The full manual of shell commands, operators, brackets, etc is here, part of the POSIX/SUS/IEEE 1003.1 specification. The Bash reference manual is here.

credit: Michael Homer
Answered July 13, 2019

The $ means: look at the end of line for the pattern The pattern is '\r\n'; meaning: \r (carage return) \n (new line)

The Square brackets [ ] are used to enclose what is being searched for on the individual line

credit: Michael S
Answered July 13, 2019
Your Answer