shell 中的环境变量问题

shell 中的环境变量问题

我正在使用 Red Hat Linux Enterprise 5。我知道这样的理论——使用 export 来设置环境变量,环境变量将应用于当前环境和子环境,但如果不使用 export 来设置环境变量,环境变量将仅适用于当前环境。

我感到困惑的是,“子环境”和“当前环境”的确切定义是什么?例如,

$ var1=123
$ echo "Hello [$var1]"

var1 的值(即 123)在 shell 中打印出来,但我认为 echo 是当前 shell 调用的命令,它(echo 命令)应该是当前 shell 的子环境,而 var1 的值不应该(因为没有使用 export var1=123)影响 echo。有什么意见吗?

提前致谢!

答案1

变量在当前环境中扩展。

$ set -x    # turn on tracing
$ var1=123
+ var1=123
$ echo "Hello [$var1]"
+ echo 'Hello [123]'
Hello [123]
$ set +x

从跟踪结果(以“+”开头的行)中可以看出,echo看到的是“Hello [123]”。它从未获取过变量。

正如你所看到的戈吉尔的回答对于您的另一个问题,导出的环境变量确实会影响子环境的环境:

$ echo $LANG
en_US.UTF-8
$ declare -p LANG   # the response includes "-x" which shows the variable is already marked for export
declare -x LANG="en_US.UTF-8"
$ ls --help | head -n 4
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort.
$ LANG=es_MX.utf8 ls --help | head -n 4
Uso: ls [OPCIÓN]... [FICHERO]...
Muestra información acerca de los ARCHIVOS (del directorio actual por defecto).
Ordena las entradas alfabéticamente si no se especifica ninguna de las opciones -cftuSUX ni --sort.

或者我可以在当前环境中设置值LANG,并且由于它已被导出,它将被子环境继承:

$ LANG=es_MX.utf8
$ grep --help | head -n 4
Modo de empleo: grep [OPCIÓN]... PATRÓN [FICHERO]...
Busca un PATRÓN en algún ARCHIVO o entrada estándar.
PATTERN es, por omisión, una expresión regular básica (BRE).
Ejemplo: grep -i '¡Hola, mundo!' menu.h main.c
$ sed --help | head -n 4
Uso: sed [OPCIÓN]... {guión-sólo-si-no-hay-otro-guión} [fichero-entrada]...

  -n, --quiet, --silent
                 suprime la muestra automática del espacio de patrones
$ while [[ = 4 ]]    # create an error on purpose to show Spanish error message
bash: se esperaba un operador binario condicional
bash: error sintáctico cerca de `4'

编辑:

这是一个简单的脚本(我们称之为showvars),以便您可以看到内部和外部发生的情况。

#!/bin/bash
arguments="$@"
printf "The script has started.\n"
printf "These are the parameters passed to the script: [$arguments]\n"
scriptvar=100
printf "This is the value of scriptvar: [$scriptvar]\n"
printf "This is the value of exportvar: [$exportvar]\n"
printf "This is the value of shellvar: [$shellvar]\n"
printf "This is the value of commandvar: [$commandvar]\n"
printf "The script has ended.\n"

现在我们在 shell 提示符下执行以下步骤:

$ shellvar=200
$ export exportvar=300
$ ./showvars 400 $shellvar 500
The script has started.
These are the parameters passed to the script: [400 200 500]
This is the value of scriptvar: [100]
This is the value of exportvar: [300]
This is the value of shellvar: []
This is the value of commandvar: []
The script has ended.
$ commandvar=600 ./showvars 400 $shellvar 500
The script has started.
These are the parameters passed to the script: [400 200 500]
This is the value of scriptvar: [100]
This is the value of exportvar: [300]
This is the value of shellvar: []
This is the value of commandvar: [600]
The script has ended.
$ printf "This is the value of commandvar: [$commandvar]\n"
This is the value of commandvar: []
$ commandvar=600
$ ./showvars 400 $shellvar 500
The script has started.
These are the parameters passed to the script: [400 200 500]
This is the value of scriptvar: [100]
This is the value of exportvar: [300]
This is the value of shellvar: []
This is the value of commandvar: []
The script has ended.
$ printf "This is the value of scriptvar: [$scriptvar]\n"
This is the value of scriptvar: []
$ printf "This is the value of exportvar: [$exportvar]\n"
This is the value of exportvar: [300]
$ printf "This is the value of shellvar: [$shellvar]\n"
This is the value of shellvar: [200]
$ printf "This is the value of commandvar: [$commandvar]\n"
This is the value of commandvar: [600]

如您所见,shellvar在脚本内部和scriptvar外部都不可用。由于exportvar已导出,因此它在脚本内部和外部都可用。并且commandvar只有在调用脚本时在命令行上传递时才在脚本内部可用。如果它是在交互式环境中设置的,然后调用脚本,则它仅在交互式环境中可用。

相关内容