環境
Ubuntu20.4
はじめに
su と su ‐ の違いについて知りたければ、ググると沢山の記事がでてきます。迷ったときはググればいいだけなのですが、本当に、記事内容の通りの動きになっているのか、自分で手を動かして確かめてみることは重要です。記事の内容を鵜呑みにすることなく、自分で手を動かして確認することで、本当の理解へとつながるものです。
違いについては、一言でいえばこの通りです。
$su - testuser # 環境変数を引き継ぎません
$su testuser # 環境変数を引き継ぎます
やったこと
適当なユーザーを新規に作成して、パスワードも設定します。
$sudo useradd testuser
$sudo passwd testuser
TESTという環境変数が登録されていないことを確認します。
$printenv | grep TEST
TESTという環境変数を作成して、01という値をセットします。
$export TEST=01
$echo $TEST
testuserユーザーに切り替えます。
$su - testuser
TEST環境変数は引き継がれていません。
$echo $TEST
(空白)
再度、testuserユーザーに切り替えます。
$su testuser
TEST環境変数が表示されます。環境変数が引き継がれているということです。
$echo $TEST
01
はい!よくわかりました。
後始末
テストのために作成したユーザーは削除します。TEST環境変数は、シャットダウンすると消えるのでそのままにしておいてOKです。
$sudo userdel testuser