-2

I am new in AWS I am using an Ubuntu server and I use export in the shell to set environmental variables.

export mykey=value

But when I close the shell and relogin I see that mykey does not available the environmental variable. As far as I search in the net, such way of setting will expire directly after shell is closed. How could I have a permanent environmental variable in AWS then? Thanks

Sami
  • 5
  • 1
  • 4

1 Answers1

0

You could edit the ~/.bashrc file by adding export mykey=value command to have it every time the shell reloads. Environmental variables are not saved when you exit all the instances of the shell. But everytime shell opens, it reads the bashrc file, if you are in a bash shell. For zsh, it would be ~/.zshrc for example.

aboitier
  • 180
  • 11
  • So you mean I always need to be logged in to my server ? I want to set some environmental variables and log out and my server just have access to those. That is it – Sami Dec 17 '20 at 13:49
  • I actually added export NODE_ENV=development then closed the shell and reopened it. Re-run the node and still it shows process.env.NODE_ENV is undefined – Sami Dec 17 '20 at 14:00
  • 1
    If these are security keys, they shouldn't be stored in files. – Raman Sailopal Dec 17 '20 at 14:03
  • If adding the export in the ~/.bashrc was still not enough to keep the exported variables after disconnection, you need to add the following code in your .bash_profile, to source the .bashrc : `if [ -f ~/.bashrc ]; then . ~/.bashrc fi` Also, can you tell me what does `echo $0`return ? – aboitier Dec 17 '20 at 14:20
  • it shows -bach. I do not udnerstand when I use echo "$NODE_ENV" it returns development but inside node ( restart node ) still process.env.NODE_ENV is undefined! Could it be because of the user? my user has root privilege but I could not edit ~/.bashrc file so I used sudo su - to switch the user to root then edit it. – Sami Dec 17 '20 at 14:56
  • It works thanks. I do not know just why with nano ~/.bashrc I could edit the file with my user. Thanks it was a big help! – Sami Dec 17 '20 at 15:02
  • Just one thing when I use NODE_ENV=production if [ -f ~/.bashrc ]; then . ~/.bashrc fi in the file when I saved it and log in again to the server I get "-bash: /home/myhome/.bashrc: line 120: syntax error: unexpected end of file " – Sami Dec 17 '20 at 22:39