Set env var in bash script - bash

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?

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

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.)

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

Related

shell program to build a stack in Linux ubuntu [duplicate]

How to set a global environment variable in a bash script?
If I do stuff like
#!/bin/bash
FOO=bar
...or
#!/bin/bash
export FOO=bar
...the vars seem to stay in the local context, whereas I'd like to keep using them after the script has finished executing.
Run your script with .
. myscript.sh
This will run the script in the current shell environment.
export governs which variables will be available to new processes, so if you say
FOO=1
export BAR=2
./runScript.sh
then $BAR will be available in the environment of runScript.sh, but $FOO will not.
When you run a shell script, it's done in a sub-shell so it cannot affect the parent shell's environment. You want to source the script by doing:
. ./setfoo.sh
This executes it in the context of the current shell, not as a sub shell.
From the bash man page:
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command executed
from filename.
If filename does not contain a slash, file names in PATH are used to
find the directory containing filename.
The file searched for in PATH need not be executable. When bash is not
in POSIX mode, the current directory is searched if no file is found
in PATH.
If the sourcepath option to the shopt builtin command is turned off,
the PATH is not searched.
If any arguments are supplied, they become the positional parameters
when filename is executed.
Otherwise the positional parameters are unchanged. The return status
is the status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or cannot
be read.
source myscript.sh is also feasible.
Description for linux command source:
source is a Unix command that evaluates the file following the command,
as a list of commands, executed in the current context
#!/bin/bash
export FOO=bar
or
#!/bin/bash
FOO=bar
export FOO
man export:
The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to word.
A common design is to have your script output a result, and require the cooperation of the caller. Then you can say, for example,
eval "$(yourscript)"
or perhaps less dangerously
cd "$(yourscript)"
This extends to tools in other languages besides shell script.
In your shell script, write the variables to another file like below and source these files in your ~/.bashrc or ~/.zshrc
echo "export FOO=bar" >> environment.sh
In your ~/.bashrc or ~/.zshrc, source it like below:
source Path-to-file/environment.sh
You can then access it globally.
FOO=bar
export FOO

How to pass output of one command as env variables to another

I have a bash script that when ran, produces output like this:
VAR1=test
VAR2=test
I want to pass these variables as environment variables to an npm script, which in this case is just running mocha in the current directory, like if I was running VAR1=test VAR2=test mocha.
Thanks in advance.
It's not a great design for a bash script to output shell variable assignments like that, but you can work around it with a wrapper script runwithvars:
#!/bin/bash
set -a # Auto-export all new variables
eval "$(mybashscript)" # Perform whichever actions the script outputs
exec "$#" # Execute the specified command
Now you can use runwithvars mocha to run mocha with those variables.
Note that if the script outputs key-value pairs instead of shell variable assignments, e.g. VAR1=some value with spaces instead of VAR1='some value with spaces', then this answer does not apply and could be fragile or dangerous.
Use export:
export VAR1=test
export VAR2=test

How to export or set a make variable from a shell file

I am running a shell script from make environment
I execute the script with input parameters as make variables:
/shell_script.sh $(make_var1) $(make_var2)
I process these variables in shell. I want to assign the result from a shell command to the make variable and export back to shell.
make_var=shell_command
How can I do this?
It is not trivial to change the parent environment of a shell-script, one approach is to echo the export statements and source the output of the script in your parent environment:
...
echo "export make_var1=${make_var1}"
...
and when you launch your script do it using eval:
eval $(./shell_script.sh $make_var1 $make_var2)
this is the approach taken by for example ssh-agent.
A second option is to source the script, in that case the script will be run line-by-line in the current shell:
. shell-script.sh
any export statements in the script will be run in the current shell.

setting bash variables while executing scripts

My sample script looks like
#!/bin/bash
VAR="foo"
if [ $VAR etc etc
then etc
...
Is there any way to call the script changing the value of $VAR without writing the script?
I would imagine something like
$ bash script.sh -v VAR="bar" (sorry for the invention)
To set an environment variable for an invocation of a program, just add the assignment to the line before you call the script:
VAR=bar ./script.sh
Note that this won't override what is set in the script; so within the script, you will need to check if it's already set before setting it to a default value.
If you want this variable to be set for several invocations, then you can define it in your shell, and export it so that it will be in the environment of child processes:
$ export VAR=bar
$ ./script.sh

Global environment variables in a shell script

How to set a global environment variable in a bash script?
If I do stuff like
#!/bin/bash
FOO=bar
...or
#!/bin/bash
export FOO=bar
...the vars seem to stay in the local context, whereas I'd like to keep using them after the script has finished executing.
Run your script with .
. myscript.sh
This will run the script in the current shell environment.
export governs which variables will be available to new processes, so if you say
FOO=1
export BAR=2
./runScript.sh
then $BAR will be available in the environment of runScript.sh, but $FOO will not.
When you run a shell script, it's done in a sub-shell so it cannot affect the parent shell's environment. You want to source the script by doing:
. ./setfoo.sh
This executes it in the context of the current shell, not as a sub shell.
From the bash man page:
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current shell
environment and return the exit status of the last command executed
from filename.
If filename does not contain a slash, file names in PATH are used to
find the directory containing filename.
The file searched for in PATH need not be executable. When bash is not
in POSIX mode, the current directory is searched if no file is found
in PATH.
If the sourcepath option to the shopt builtin command is turned off,
the PATH is not searched.
If any arguments are supplied, they become the positional parameters
when filename is executed.
Otherwise the positional parameters are unchanged. The return status
is the status of the last command exited within the script (0 if no
commands are executed), and false if filename is not found or cannot
be read.
source myscript.sh is also feasible.
Description for linux command source:
source is a Unix command that evaluates the file following the command,
as a list of commands, executed in the current context
#!/bin/bash
export FOO=bar
or
#!/bin/bash
FOO=bar
export FOO
man export:
The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to word.
A common design is to have your script output a result, and require the cooperation of the caller. Then you can say, for example,
eval "$(yourscript)"
or perhaps less dangerously
cd "$(yourscript)"
This extends to tools in other languages besides shell script.
In your shell script, write the variables to another file like below and source these files in your ~/.bashrc or ~/.zshrc
echo "export FOO=bar" >> environment.sh
In your ~/.bashrc or ~/.zshrc, source it like below:
source Path-to-file/environment.sh
You can then access it globally.
FOO=bar
export FOO

Resources