Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set env var in bash script

Tags:

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?

like image 325
alfredopacino Avatar asked Jul 11 '26 11:07

alfredopacino


1 Answers

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
like image 81
Gonzalo Matheu Avatar answered Jul 14 '26 04:07

Gonzalo Matheu