Krishna Reddy
Krishna Reddy

Reputation: 3

Regarding sed expression evaluation in shell

Could someone explain the meaning of below sed statement?

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

Upvotes: 0

Views: 228

Answers (1)

fedorqui
fedorqui

Reputation: 290015

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:.

Upvotes: 2

Related Questions