This question already has answers here:
Display all environment variables from a running PowerShell script
(10 answers)
How to print environment variables to the console in PowerShell?
(5 answers)
Closed 4 months ago.
When entering echo path on PowerShell on my Windows 11, nothing proper is showing up.
I recall Windows 10 shows all environmental path variables, but not anymore?
ECHO %PATH% returns the value of the PATH environment variable when run in cmd.exe, but not PowerShell.
The PowerShell command you're looking for is as follows:
$env:PATH
If you need to list all environment variables in PowerShell, use gci env:. The equivalent command for cmd.exe is SET.
Please see this Stack Overflow post for more information.
Thanks to #mklement0 for the clarification.
Related
This question already has answers here:
Why is no string output with 'echo %var%' after using 'set var = text' command in cmd? [duplicate]
(2 answers)
Closed 4 years ago.
The below code, for some reasons, is not working. I'm kinda new to this Windows Batch area and don't have any clue why this is not working. Sounds pretty simple and correct to me but something has gone wrong. Can you please help me?
I'm running it on a Windows 10 machine.
#echo off
setlocal enabledelayedexpansion
SET pathOfFileName1 = C:\test\Dump_1_333398395823532298.zip
echo %pathOfFileName1%
PowerShell Expand-Archive "%pathOfFileName1%" "C:\test\unzip"
The pathOfFileName1 is not getting printed and the PowerShell stuff is not working when used with the variable 'pathOfFileName1'.
Powershell stuff is working when both parameters are used without any variables.
echo is working when the value is directly given (instead of the variable)
Appreciate your inputs.
Thanks
SET pathOfFileName1 = C:\test\Dump_1_333398395823532298.zip
This line is actually setting an enviornment variable called "pathOfFileName1 ". Note the space after the variable name.
You probably want to use this instead
SET pathOfFileName1=C:\test\Dump_1_333398395823532298.zip
The lack of spaces before or after the equals sign are important. You don't want a space at the end of the variable name, or a space at the start of the string.
This question already has answers here:
How to set environment variables in Python?
(19 answers)
Reading and writing environment variables in Python? [duplicate]
(4 answers)
set environment variable in python script
(3 answers)
Closed 5 years ago.
In terminal (bash) on OSX I can set an environment variable, using the syntax export VARNAME=1 or export VARNAME="hello"; and that persist as long as the session is running, or until the terminal window is closed.
What would be the equivalent form, to do the same via Python3? I would like to avoid to call Popen just to set a global variable.
Also I need this variable only for the purpose to run my python code; once the script is done, I do not need it anymore; so even if it last only for the lifespan of my script running, it is acceptable.
This question already has answers here:
where is $PATH set? Specifically where is my mac port path being set? [closed]
(2 answers)
Closed 6 years ago.
I used a program that set system setting and I can't find where my PATH environment variable was set, it's not set in:
.bashrc
.bash_profile
.profile
etc/paths
I have been trying to use a grep command:
grep -rl "PATH=" /
but the command is taking forever.
Does anyone have any suggestion on searching for where my PATH was assigned?
Run the following command:
PS4='+ $BASH_SOURCE:$LINENO:' BASH_XTRACEFD=7 bash -xlic "" 7>trace.out
Now, look through the file trace.out. It will show you everytime PATH was modified along with the file name and line number which caused the change.
Example
$ grep PATH trace.out
+ /etc/profile:7:PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
+ /etc/profile:9:export PATH
This tells you that line 7 of the file /etc/profile set the PATH.
How it works
The short story is that this starts up a bash login session with tracing turned on. For a longer explanation, see here.
This question already has answers here:
Can I export a variable to the environment from a Bash script without sourcing it?
(13 answers)
Closed 3 years ago.
I have a simple environment setup script that exports some environment variables like so.
#!/bin/sh
export NEWROOT=~/some/directory
echo $NEWROOT
This echos the correct directory, but after its run, when I echo $NEWROOT in the same shell, it returns nothing.
Any idea why the variable isn't setting?
The shell is run in a separate process, and environment variables in a child process do not affect the environment variables in the parent process.
If you want to run the script in the same process, you can use the dot command, like this:
. myscript
A child process can't affect the environment variables of its parent. If you source the script instead, that will evaluate the script in the current environment, leaving NEWROOT.
This question already has an answer here:
Best way to set environment variables in calling shell
(1 answer)
Closed 8 years ago.
I have a script "set_var.sh" written like this
#!/bin/bash
export NAME=release
export ROOT=/Volumes/name/dev/release
but if I run this set_var.sh from terminal, afterward I issue set command to check variables I could not find NAME and ROOT var be set.
I am wondering what is wrong in my case.
it was set in sub-shell.
you need
source set_var.sh
If you simply run set_var.sh, it runs in its own shell which exits, losing the variables that were set.
If you want to change variables in your interactive shell, you can use:
source set_var.sh
or the shorthand,
. set_var.sh
This will execute the lines of the script as if they were typed into your interactive shell.
Note that when you "source" a file this way, it does not require the "shebang" on the first line.
Note also that this is feature exists in Bourne shell as well, but only in the short-form version.