7

I want to set 2 temp env vars and then run a binary.

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:

setparams.sh my_binary

setparams.sh

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

What's wrong here? Why do the vars are not being set?

7
  • The working command is syntactic; a command consists of optional pre-command modifiers plus the actual command. Your attempt is just a script that takes my_binary as an argument and ignores it, while setting variables that will disappear on exit. Commented Aug 6, 2020 at 21:01
  • Your "script" would just be ENV_1=firstparam ENV_2=secondparam "$1". Commented Aug 6, 2020 at 21:01
  • What you have are variable assignments. You are not working with environment variables at all; the variables you have are internal to the shell Commented Aug 6, 2020 at 21:24
  • 1
    @chepner Using "$@" rather than "$1" lets you pass additional arguments to the command. Commented Aug 6, 2020 at 21:29
  • @WilliamPursell In the first command in the question ENV_1=firstparam ENV_2=secondparam my_binary the variables are passed as environment variables to my_binary (but they don't persist after that). Commented Aug 6, 2020 at 21:29

3 Answers 3

14

By default all user defined variables are local. They are not exported to new processes. Use export command to export variables and functions

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 setparams.sh && my_binary
Sign up to request clarification or add additional context in comments.

Comments

8

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.

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.

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.

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 or source ./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.)

1 Comment

despite I was talking about env vars, I meant just set some variables for the subsequent command.
0

You need to export your environment variables:

setparams.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:

firstparam

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.