4

so I have create setup.sh file like this :

#!/bin/bash
export SP_TWITTER_KEY=adsfad
export SP_TWITTER_SECRET=aadsfadsf
export SP_TWITTER_ACCESSTOKEN=asdfasdfa
export SP_TWITTER_ACCESSSECRET=asdfasdfas

export mongo_connection=localhost

and after that I run this command in order to turn the file to be executable :

$ chmod +x setup.sh

finally execute the file so that all my env varible are exported. I have tried several ways like this :

$ . /setup.sh
$ .setup.sh
$ bash setup.sh

But none of them are working, because I check again the variable like mongo_connection and others are still empty. Any idea what's wrong or am I missing something?

NOTE : I'm using tmux to run my shell terminal

Gujarat Santana
  • 413
  • 1
  • 5
  • 13

1 Answers1

10

Without any change into your script you can simply run in like this:

. setup.sh

or

source setup.sh

it will cause your variables to be set in the current shell otherwise bash will open a non-interactive shell, runs your command into that and after it's finish, it closes that shell.

for more explanation, help source:

source: source FILENAME
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell.

Ravexina
  • 55,668
  • 25
  • 164
  • 183
  • it looks like my problem is when I execute my setup.sh in another shell on tmux. It doesnt export on the other shell – Gujarat Santana May 21 '17 at 09:09
  • 1
    When you export a variable it will only be available in child process of that shell not others, if you want to set something globally you can put it in places like .bashrc . – Ravexina May 21 '17 at 09:10