2

I've written a shell script, called credentials.sh, which contains:

export USERNAME = 'user'
export PASSWORD = 'pass'

Security of these variables is no big deal.

Two questions.

  1. When I run this script, ./credentials.sh (both as root and not), it doesn't actually export the vars. Why is this?
  2. What is the preferred way to do so they are universally available (Ubuntu 14.04)?
cjm2671
  • 18,348
  • 31
  • 102
  • 161
  • 5
    Don't run it. Source it. `. credentials.sh` instead of `./credentials.sh`. You need to do that to run it in the current shell and not a new shell. – Etan Reisner Mar 25 '15 at 23:36
  • 5
    Your shell syntax is wrong in that it should be `USERNAME='user'` -- i.e. no space. This is actually important. – Explosion Pills Mar 25 '15 at 23:37
  • Run it as a part of the current shell by . ./credentials.sh. That is a dot followed by the file name. – kjohri Mar 26 '15 at 01:19
  • See also http://stackoverflow.com/questions/496702/can-a-shell-script-set-environment-variables-of-the-calling-shell – Charles Duffy Jun 02 '15 at 15:08
  • BTW, if you control the variable names, you should consider using ones less likely to conflict with other programs/scripts. – Charles Duffy Jun 02 '15 at 15:13

1 Answers1

3

Two changes:

  1. Fix your variable assignment syntax:

    export USERNAME='user'
    
  2. To make this value always available to user, append the above code line to the file ~/.profile; this will set the variables during login, such that this will not need to be manually invoked at other times.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
M S Parmar
  • 955
  • 8
  • 22