What is the cleanest way to modify this command in bash to only run if the group does not exist?

groupadd somegroupname

A one-liner would be best.

2 Answers 2

getent group somegroupname || groupadd somegroupname
  • 4
    Thanks. In case it helps anyone, this slight modification avoids printing output from the first command: [ $(getent group somegroupname) ] || groupadd somegroupname Commented Jul 18, 2014 at 20:06
  • 2
    Or just redirect it's output to /dev/null Commented Jul 18, 2014 at 20:11

With force it exits successfully if the group already exists, and cancels -g if the GID is already used.

groupadd -f somegroupname

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .