29

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.

Oleg Pryadko
  • 2,400

2 Answers2

46
getent group somegroupname || groupadd somegroupname
glenn jackman
  • 85,964
15

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

groupadd -f somegroupname
ram4nd
  • 277
  • 2
  • 7