How do I get and print value of an environment variable? - windows

I want to print value from a Windows environment variable, say, path or errorlevel, I've tried this but it doesn't work. Output this in my console:
(without consider spaces/tabs which it outputs):
echo %PATH
%PATH
Makefile:
PATH=$(PATH);\nonesuch
all:
echo %PATH%
command-line:
nmake /E
How do I fix it?
NOTE: Visual Studio's binary path is in my PATH variable, that's why I'm calling this outside VS console

The percent sign % has special meaning in Makefiles.
In order to perform the Windows batch-file substitution, you need to escape it like this:
echo %%PATH%%
This seems to work too:
"echo %PATH%"
Another option is to perform the substitution on the Make side, but that's a different thing:
echo $(PATH)

To view your path, just try out this
echo $PATH

Related

shell script capture variable without formatting escape characters

I am running an .sh script from wsl bash.
Inside the script I want capture a file path, into a variable, in order to pass it to cmd.exe like so:
my_win_path=<some_windows_unc_path_to_executable>
cmd.exe /c "$my_win_path"
However when I try to capture the file path, it's backslashes are getting formatted, resulting in an non-valid path.
Example:
~: wslpath -w .
C:\tmp
~: var=$(wslpath -w .); echo $var
C: mp
cmd.exe /c "$var # will error
How do I capture the output of wslpath into a valid variable without formatting the escape characters ?
(I hope I am using the right terminology here. Please correct me if not.)

echo %cd% alias does not respond as it should

I have created and alias named pwd for echo %cd% (#DOSKEY pwd=echo %cd%). I have saved it in bat file and configured it to autorun with command processor.
Now whenever I run the command pwd in my command processor it returns with the C:\windows\system32 no matter in which path I am currently in. whereas when I run the echo %cd% it returns the right path I am in.
How do I solve this problem? Is it because of the parameter I am passing to echo? This parameter should update according to the path I am going in. It seems it updates just once when the command prompt is loaded with aliases.
That is because %cd% is expanded during the definition of the macro, not when it is executed.
From a batch file, you should use:
#DOSKEY pwd=echo %%cd%%
If defining from the command line, the expansion rules are different, so you would need something like:
DOSKEY pwd=echo %^cd%
But there is an even simpler method that works in all cases. The CD command without arguments simply lists the current directory. Just enclose the command in parentheses to prevent arguments from being passed.
#DOSKEY pwd=(cd)

How do I conditionally turn #ECHO ON/OFF in a Windows batch file?

I have a batch file that can be run locally or on the build server to do some kind of interesting job. On the local machine, I want #ECHO OFF so that your console isn't full of debug strings. On the build server, I'd rather have #ECHO ON so that failures can be investigated.
The build server context can be determined if a special environment variable exists (TEAMCITY_PROJECT_NAME). So, I thought I could do something like
IF DEFINED TEAMCITY_PROJECT_NAME #ECHO ON ELSE #ECHO OFF
But, that's not working, I get the IF statement echoed along with everything else... any thoughts?
The command is not actually named #echo. The # is an optional flag valid at the beginning of a statement to indicate that echo is suppressed for that statement. So using the # on each reference to the echo command will hide the echo command itself, but not the if command that precedes it.
But the issue that is actually causing your command to fail to operated as expected is that the first echo command will slurp up the rest of the tokens on the line as its arguments, so the else echo off part will not be interpreted by CMD. Since CMD.EXE echoes by default it prints the if command, and then either executes a single echo command or nothing. Since the # does have meaning at the start of the statement that is the body of the if, neither echo command would be printed.
In general, the solution to that is to use parenthesis to delimit the command boundaries. However, since echo is on by default and you really only want to suppress it if TEAMCITY_PROJECT is not define, we can say that directly.
#IF NOT DEFINED TEAMCITY_PROJECT_NAME ECHO OFF
I've left a single # at the beginning to suppress echo of this line, and as a result this line will not appear in your server's logs.
Related note
The echo state also applies to an interactive session. Typing echo off at an interactive CMD session prompt will cause it to stop showing the prompt. Which is a tad disconcerting, if not expected. Typing echo on will restore normal behavior.
More on Parsing
(I've edited the earlier section for clarity, and added this section to attempt to document what is really happening.)
The CMD.EXE program that interprets .BAT and .CMD scripts, and provides the interactive command prompt in modern Windows is surprisingly well documented while effectively being undocumented. My attempts to search for an official document explaining that the at-sign has this effect and exactly where it can be used have been largely unsuccessful.
It is clear from experimentation that the at-sign is parsed and interpreted if it appears as the first non whitespace character of a command. I've tried to express that by using the phrase "beginning of a statement" above.
As far as I can tell there is no formal grammar published for CMD's language. But we do know from the documentation for CDM itself (type cmd /? to a prompt) as well as if /? and the built-in help text for other "interesting" built-in commands that there are rules that are followed when CMD parses its source text.
The start of a command appears to be at the beginning of a line, or after the conditional of an if, after the else, or after an open parenthesis (. Once the at-sign has been recognized, it applies to (most) of the balance of that command.
Try the following batch file yourself, and play with moving the at signs around and you'll quickly get the sense that the rules are hard to state precisely:
rem this remark will echo
#rem this one will not
# rem neither will this
#rem nor this one
#rem the if command (and else) will echo, but not either echo command
if exist q17241089.bat ( # echo saw q17241089.bat ) else # echo foo
if not exist q17241089.bat ( # echo no q17241089.bat ) else # echo bar
# rem none of the following command is echoed
# if exist q17241089.bat ( # echo saw q17241089.bat ) else # echo spam
When run on my Win 7 Pro I see:
C...>
C...>q17241089.bat
C...>rem this remark will echo
C...>if exist q17241089.bat () else
saw q17241089.bat
C...>if not exist q17241089.bat () else
bar
saw q17241089.bat
C...>
As with most of the fine details of BAT files, there is a mystery underneath any surface you scratch.
set parentheses (see docu...):
IF DEFINED TEAMCITY_PROJECT_NAME (#ECHO ON) ELSE #ECHO OFF
Well, it seems that ECHO is on by default, so you'll get the IF statement output anyway you test that conditional. You should start with
#ECHO OFF
And add your conditional as another statement to evaluate
#ECHO OFF && IF DEFINED TEAMCITY_PROJECT_NAME #ECHO ON
As noted in earlier responses, ECHO is on by default. An alternative way of achieving the same behavior is:
#if "%TEAMCITY_PROJECT_NAME%" == "" #echo off

How is it possible to view windows environment variable without executing arguments within it?

Basically I would like to export my exact PATH variable to a file automatically. It contains things like %ANT_HOME%/bin and I would like to keep it that way. From what I could find, using both set and echo will execute that argument and give me the absolute path. Is there something I'm missing?
To get a copy of your PATH without expansion of environment variables you could save the following as "rawPath.vbs"...
Option Explicit
Dim wsh
Set wsh = CreateObject("Wscript.Shell")
Wscript.Echo wsh.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
...and then issue the following command to pipe the output to a file
cscript -nologo rawPath.vbs > myPath.txt
Do you see %ANT_HOME% when you execute SET from the prompt?
If so,
>filename echo %path%
should export the path as desired.
If the PATH variable does not actually contain the "%" characters, then it's already been resolved. And remember, "%" is actually a legitimate (but annoying) filename character...
You CAN set a "%" character into an environment variable
set var=%%something%%
will set var to %something%
You need to escape the % good sir, example
>echo %path%
C:\windows\system32;C:\windows\system32\wbem
>echo ^%path^%
%path%

Windows Command Line: Non Evaluation of Environment Variable

I would like to provide the raw text referring to an environment variable to a command instead of evaluating the environment variable.
I need this to configure BizTalk from the command line, for example:
BTSTask.exe AddResource -ApplicationName:App1
-Type:System.BizTalk:BizTalkAssembly -Overwrite
-Source:..\Schemas\bin\development\App1.Schemas.dll
-Destination:%BTAD_InstallDir%\App1.Schemas.dll
This command adds a resource to a BizTalk application. I want the destination to be %BTAD_InstallDir%\App1.Schemas.dll, however at present it is evaluating the environment variable (to nothing) and using \App1.Schemas.dll.
Is it possible to escape or disable the evaluation of this environment variable while parsing\executing this command?
I have tried escaping the first and both percentage characters with a carrot (^), however this did not stop the evaluation.
[EDIT] When I execute this at the command prompt it doesn't replace the environment variable, however it does when I run it as a script, any thoughts as to why this is different?
Try echo ^%path^% in a command prompt it prints...
path
instead of expanding the environment variable so I guess the following should work for you as suggested by Mikeage
BTSTask.exe AddResource -ApplicationName:App1 -Type:System.BizTalk:BizTalkAssembly -Overwrite -Source:..\Schemas\bin\development\App1.Schemas.dll -Destination:^%BTAD_InstallDir^%\App1.Schemas.dll
Did you try:
%%BTAD_InstallDir%%
in your script ?
That should prevent the script to interpret the variable, and it would pass %BTAD_InstallDir% to the program.
Try ^% instead of %.
Tried:
C:\PrgCmdLine\Unix\echo.exe "%"JAVA_HOME"%"
Got:
%JAVA_HOME%
[EDIT] Indeed, C:\PrgCmdLine\Unix\echo.exe ^%JAVA_HOME^% works too, and is simpler...
[EDIT 2] For the record: I used UnxUtils' echo to have the behavior of a plain program. Built in echo has a slightly different behavior, at least for quoted % signs.
Not sure if it's the same as my case, but i was troubling to use a batch file to create a script which has %temp% variable inside.
The workaround i found:
set test=%temp;
echo {command} %test%%>>path_to_my_batch_file;
Hope this helps someone:)

Resources