0

Could someone explain the meaning of below sed statement?

sed -i "s/PS1\='\\\\u\@[^]]*:/PS1\='\\\\u\@\\\\H:/g" test
Thor
  • 45,082
  • 11
  • 119
  • 130

1 Answers1

2

First of all, note that PS1 is the bash prompt. See How to: Change / Setup bash custom prompt (PS1) for more references.

sed -i "s/PS1\='\\\\u\@[^]]*:/PS1\='\\\\u\@\\\\H:/g" test

It looks for the text PS1\='\\u\@[^]]*: and replaces it with PS1\='\\u\@\\H: in test file.

sed 's/hello/bye/g' file is the basic sed command that looks for hello and replaces it with bye all along the file (g means "global", so it does every time it finds the text).

While this sed expression shows the result on stdout, if you want the result to update the file, you add the -i option instead.

Then, note that I mentioned that the text looked for is PS1\='\\u\@[^]]*:, while in the sed expression we see PS1\='\\\\u\@[^]]*:. That's why any \ has to be escaped... and the \ character is used to do so.

Regarding the specific pattern looked for:

PS1\='\\u\@[^]]*:

means text like

PS1='\\u\@` 
+
any string until the character `]` is found
+
:

So it will match texts like PS1\='\\u\@[hello how are you]:.

It replaces them with PS1\='\\u\@\\H:.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Could you please elaborate which pattern it replaces with what. example – Krishna Reddy Aug 22 '13 at 09:46
  • Are you sure it's looking for `PS1\='\\u\@[^]]*:` and not `PS1='\\u@[^]]*:` i.e. without escapes in front of the `=` and `@`? I think `\` in the search RE means `a literal ` rather than ` followed by ` as you'd write that as `\\` but I'm not 100% sure how sed treats it if you escape characters like `=` and `@` that don't need to be escaped as they aren't RE meta-characters. – Ed Morton Aug 22 '13 at 13:11