Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set an environment variable?

Tags:

npm

In bash I can see npm environment variables with npm run env. USER=brianmackey is one such environment variable. How can I set an environment variable, say USER=billybob?

I know I can use npm config set <key> <value> [--global]. Is key+value always/in any case an environment variable? Can I set the environment variables in session?

like image 925
P.Brian.Mackey Avatar asked Apr 09 '15 19:04

P.Brian.Mackey


People also ask

What does setting an environment variable mean?

An environment variable is a variable whose value is set outside the program, typically through functionality built into the operating system or microservice. An environment variable is made up of a name/value pair, and any number may be created and available for reference at a point in time.


1 Answers

Single Command

If you want to set environment variables for a single node command, you can simply do this:

$ USER=billybob node server.js

Loaded for each session

If you want to permanently set that environment variable for your user, edit your ~/.bash_profile and add the following line:

export USER="billybob"

This will automatically set the given environment variable each time you create a new terminal session.

Existing for the entire current session

Lastly, if you want to set the environment variable only for the current session, just run it as it's own command:

$ USER=billybob
$ node app.js # user is billybob
$ node app.js # user is still billybob

When you exit the session, these temporarily set environment variables will be cleared.

like image 185
xterminator Avatar answered Dec 25 '22 10:12

xterminator