简体   繁体   中英

what is meaning of sed -e 's|PATH="\(.*\)"|PATH="/opt/man/common/bin:\1"|g' -i /etc/environment line

以下命令是什么意思?

sed -e 's|PATH="\(.*\)"|PATH="/opt/man/common/bin:\1"|g' -i /etc/environment

It substitutes all the instances of

PATH="somestring"

with

PATH="/opt/man/common/bin:somestring"

in the file /etc/environment

In detail

  • s|string1|string2| substitutes string1 with string2
    • the delimiter (in this case | ) is defined by the character following the substitute command ( s ). More common is / .
    • the flag g at the end tells sed to substitute all non-overlapping matches (and not only the first one)
    • the \( and \) define a group (in this case everything between the quotes)
    • the \1 is a back-reference to the first group
  • -i is telling sed to apply the changes directly in the file (inline) instead of writing to standard output.

Edit

As pointed out in the comments this regular expression is very fragile.

  • It will match the string anywhere (not only after a blank or the beginning of a line). It will for example match CDPATH="string"
  • It will not match strings delimited by ' . It will not match PATH='somepath' .
  • It will match strings in comments. For example # some comment with PATH="" in the text
  • It will match inside other strings, for example STRING='PATH="somepath"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM