.bashrc
で気づきました 一部の行にはexport
があります それらの前に、たとえば
export HISTTIMEFORMAT="%b-%d %H:%M "
...
export MYSQL_HISTFILE="/root/.mysql_history"
など、他の人はそうではありません
HISTSIZE=100000
まず、これが正しいかどうか、次にexport
を使用するためのルールは何か疑問に思っています。 .bashrc
内 。
承認された回答:
必要なのはexport
だけです シェル内で起動する他のプログラムによって「表示」される必要がある変数の場合、シェル自体の内部でのみ使用される変数はexport
である必要はありません。 ed。
これはmanページの内容です:
The supplied names are marked for automatic export to the environ‐
ment of subsequently executed commands. If the -f option is given,
the names refer to functions. If no names are given, or if the -p
option is supplied, a list of all names that are exported in this
shell is printed. The -n option causes the export property to be
removed from each name. If a variable name is followed by =word,
the value of the variable is set to word. export returns an exit
status of 0 unless an invalid option is encountered, one of the
names is not a valid shell variable name, or -f is supplied with a
name that is not a function.
これは、次のように実証できます。
$ MYVAR="value"
$ echo ${MYVAR}
value
$ echo 'echo ${MYVAR}' > echo.sh
$ chmod +x echo.sh
$ ./echo.sh
$ export MYVAR="value-exported"
$ ./echo.sh
value-exported
説明:
- 最初に
${MYVAR}
を設定しましたMYVAR="value"
を持つシェル変数になります 。echo
を使用する エコーはシェルの一部であるため、その値をエコーできます。 - 次に、
echo.sh
を作成します 。これは基本的に同じことを行う小さなスクリプトで、${MYVAR}
をエコーするだけです。 、ただし、別のスクリプトであるため、別のプロセスで実行される点が異なります。 -
echo.sh
を呼び出す場合 新しいプロセスは${MYVAR}
を継承しないため、何も出力されません。 - 次に、
${MYVAR}
をエクスポートしますexport
を使用して私の環境に キーワード - 同じ
echo.sh
を実行すると ここでも、${MYVAR}
のコンテンツをエコーします 環境から取得するため
だからあなたの質問に答えるために:
変数をエクスポートする必要があるかどうかに関係なく、変数が使用される場所によって異なります。
関連:Googleチャットログをエクスポートする方法は??