lorless
lorless

Reputation: 4478

How can I set and use an environment variable in windows cmd in the same line?

A bit of background, on mac you can do something like this

PORT=3002 NODE_ENV=dev node server.js

But can you do the same on windows? I have tried

set PORT=3002 && echo %PORT%

but the variable does not update until the echo is called again.

The reason I need this is that i am currently using npm start to run a script on a mac that looks like the first line of code. This does not work in windows since you cannot set env variables like that.

Upvotes: 0

Views: 100

Answers (2)

Magoo
Magoo

Reputation: 79983

@echo off
setlocal enabledelayedexpansion
set something=whatever&echo something=!something!

But you seem to have no reason for wanting this on a single line.

Upvotes: 0

Constance Garcia
Constance Garcia

Reputation: 41

In most contexts, surround the variable name with %'s and the variable's value will be used e.g. To display the value of the _department variable with the ECHO command:

ECHO %_department%

Upvotes: 4

Related Questions