Can someone explain me these "sed" commands and what they are doing?

Can someone give me a quick explanation what these lines are doing and what they effects are?

1) sed -i "/^exampleusername/s/:1000:1000:/:${UID}:${GID}:/g" /etc/passwd

2) sed -i "/^examplegroupname/s/:1000:/:${GID}:/g" /etc/group

I saw them here if you are interested in more context.

1 answer

  • answered 2018-04-14 15:47 that other guy

    You should check out a sed tutorial or man page. Here is #1 broken down (and no longer valid sed). The second one has the same general form:

    /^exampleusername/      # Perform a command on matching lines (address)
        s/                  # Substitute command with / as delimiter
          :1000:1000:       # Pattern to find
          /                 # Delimiter
          :${UID}:${GID}:   # String to replace with (expanded by bash)
          /                 # Terminating delimiter
          g                 # Regex flag (global: replace all matches in line)

    So it just finds the line that starts with exampleusername and replaces all occurrences of :1000:1000: with your current user's UID/GID.

    This effectively makes exampleusername the same as your current user.