简体   繁体   English

在 bash 脚本中设置环境变量

[英]Set env var in bash script

I have to admit I'm very rusty with bash scripting.我不得不承认我对 bash 脚本非常生疏。 I want to set 2 temp env vars and then run a binary.我想设置 2 个临时环境变量,然后运行一个二进制文件。

The command is like this:命令是这样的:

ENV_1=firstparam ENV_2=secondparam my_binary

I want to move the 2 env var assignment in a bash script and use a command like this:我想在 bash 脚本中移动 2 env var 分配并使用如下命令:

setparams.sh my_binary

setparams.sh设置参数.sh

#!/bin/bash
ENV_1=firstparam
ENV_2=secondparam

What's wrong here?这里有什么问题? Why do the vars are not being set?为什么没有设置变量?

By default all user defined variables are local.默认情况下,所有用户定义的变量都是本地的。 They are not exported to new processes.它们不会导出到新流程。 Use export command to export variables and functions使用export命令导出变量和函数

export ENV_1=firstparam
export ENV_2=secondparam

Also, instead of executing you should call source (built-in command that executes the content of the file passed as argument, in the current shell):此外,您应该调用source (在当前 shell 中执行作为参数传递的文件内容的内置命令),而不是执行:

source setparams.sh && my_binary

Since you're passing my_binary as an argument to the script, I assume you want the script to (a) set the environment variables and then (b) invoke the command you sent it.由于您将my_binary作为参数传递给脚本,因此我假设您希望脚本 (a) 设置环境变量,然后 (b) 调用您发送的命令。

One way to do that is:一种方法是:

#!/bin/bash

ENV_1=firstparam ENV_2=secondparam "$@"

"$@" expands to the list of arguments you passed to the script. "$@" 扩展为您传递给脚本的 arguments 列表。

If you set variables like that, they'll be inherited in the environment of any command you run on the same command line , but not by any subsequent commands.如果您像这样设置变量,它们将在您在同一命令行上运行的任何命令的环境中被继承,但不会被任何后续命令继承。

If you wanted to execute more than one command with those environment variables, you could do:如果您想使用这些环境变量执行多个命令,您可以执行以下操作:

#!/bin/bash

export ENV_1=firstparam
export ENV_2=secondparam
some_command
some_other_command

Then $ENV_1 and $ENV_2 will appear in the environment of some_command and some_other_command -- but not in the environment of your shell after set_params.sh finishes.然后$ENV_1$ENV_2将出现在some_commandsome_other_command的环境中——但在set_params.sh完成后不会出现在您的 shell 的环境中。

If you want a script to set environment variables that will be available in your interactive shell, you'll have to invoke it with . ./set_params.sh如果您想要一个脚本来设置将在您的交互式 shell 中可用的环境变量,您必须使用. ./set_params.sh . ./set_params.sh or source./set_params.sh . . ./set_params.shsource./set_params.sh (And in that case you don't need the #!/bin/bash at the top, since it will execute in your current shell.) (在这种情况下,您不需要顶部的#!/bin/bash ,因为它将在您当前的 shell 中执行。)

You need to export your environment variables:您需要导出环境变量:

setparams.sh设置参数.sh

#!/usr/bin/env bash

# Export environment variables
export ENV_1=firstparam
export ENV_2=secondparam

# Launch binary with its provided arguments
"$@"

Testing it:测试它:

./setparams.sh bash -c 'echo "$ENV_1"'

Output: Output:

firstparam

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM