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?

スクリプト実行時に"この変数は読み取り専用です"エラーが出た

Last updated at Posted at 2023-08-23

次のようにUID変数にid -u結果を取得して処理を実行しようとした際に、タイトルにあるようなエラーが出て少し詰まったので記録。

#!/bin/bash

UID=`id -u`
echo "user id -> ${UID}"

原因

UIDはシステムで定義済みの環境変数だったようです。
https://www.ibm.com/docs/ja/i/7.2?topic=language-shell-variables
この他にも知らなかった定義済み変数があったので、この機会に勉強になりました。

解決策

  • 定義済み変数をそのまま使う
#!/bin/bash

echo "user id -> ${UID}"
  • 定義されていない変数名を使う
#!/bin/bash

USER_ID=`id -u`

echo "user id -> ${USER_ID}"
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?