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.

getent group somegroupname || groupadd somegroupname
| improve this answer | |
  • 3
    Thanks. In case it helps anyone, this slight modification avoids printing output from the first command: [ $(getent group somegroupname) ] || groupadd somegroupname – Oleg Jul 18 '14 at 20:06
  • 2
    Or just redirect it's output to /dev/null – glenn jackman Jul 18 '14 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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.