0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

macOSには`sg`コマンドがない。

Posted at

はじめに

Linuxにはsgというコマンドがありまして1

sgを使うことで一時的に別のグループでコマンドを実行できるわけです。

たとえば

sg mygroup -c 'echo $(id -g -n)'

とすれば、

mygroup

と表示されるわけです。

でもmacOSのターミナルで同じことを実行しようとすると…

zsh: command not found: sg

無情にもsgコマンドが見つかりません。

解決策: newgrpを使う

newgrpは単独で実行するとexitするまで指定したグループでコマンドを実行できるという優れものですが、そのままではシェルスクリプトで使うには不便です。
でも、大丈夫。
newgrpの標準入力にコマンドを渡すと、sgと同じようにその時だけ別のグループでそれを実行してくれるのです。
具体的には次のようにします:

echo 'echo $(id -g -n)' | newgrp mygroup
mygroup

やったぜ👍

そして、newgrpはLinuxにも存在しているので、macOSでもLinuxでも両方で動くシェルスクリプトを書きたければ、newgrpを使えばOK。

という小ネタでした。

  1. https://man7.org/linux/man-pages/man1/sg.1.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?