Viewing Environment Variables in VS Code: Pay attention to the Terminal Type
Here’s a quick tip based on a recent mistake I made while trying to view an environment variable in the VS Code Terminal.
I’m still getting familiar with VS Code and recently made a pretty basic mistake. I was trying to view the value of an environment variable. Having developed for Windows on and off for many years, I’m more familiar with Visual Studio. My first instinct when trying to view the environment variable in the VS Code Terminal was to try to view the variable using the command line syntax. Of course, this didn’t work. After a few minutes of confusion, I realized the VS Code Terminal defaults to PowerShell.
In a command window, I would type echo %VARNAME%
, e.g. echo %PATH%
, to view an environment variable. This equivalent PowerShell command is $Env:VARNAME
. Once I switched to the correct syntax —$Env:MYVAR
— then everything worked fine. Lesson learned.
Since this blog is as much to educate myself as it is to help other people, I thought I’d take another look at the terminal options in VS Code.
First, the type of terminal window that is opened can be seen in the upper right corner of the terminal window. Something I didn’t notice when I was trying to view the environment variable using command prompt syntax.
Use the ➕ drop down to open a new terminal window and choose the terminal type.
An important part of software development is knowing how to use your tools. So, I’m off to read the Terminal Basics page of the Visual Studio docs, and then the rest of the docs as well. Hopefully, I can avoid any more basic mistakes like this one. And if this saves someone else a few minutes of confusion—mission accomplished.