How to pass the name of environment variable as an argument?

dark9duck

I have a batch.bat with one input argument %1. I want to start the script as: batch.bat PATH. I would like this to display the PATH environment variable with echo.

I tried:

@echo %1
E:\>batch.bat PATH
PATH

but I would like to have the content of PATH environment variable.

E:\>batch.bat PATH
C:\Windows;   
user6811411

You need delayed expansion to output a variable referenced by a cmd line argument.
This batch uses two different methods for this,

  • a pseudo call with doubled percent signs (which forces the parser to evaluate the line twice, on first pass reducing two %% to one in 2nd pass the other vars are already expanded) and
  • using the ! instead the normal single percent sign with a prior command setlocal enabledelayedexpansion

:: Q:\Test\2018\05\10\SO_50280684.cmd
@Echo off&SetLocal EnableDelayedExpansion
Echo(%1
Echo(!%1!
Call Echo(%%%1%%

I use the command separator ( instead off a space to suppress an Echo is off message incase of an empty var.

Sample with command

>SO_50280684.cmd PROCESSOR_ARCHITECTURE
PROCESSOR_ARCHITECTURE
AMD64
AMD64

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass a list as an environment variable?

How to pass argument name as a parameter?

How to pass a variable as a command line argument in iPython?

Python: how to pass argument name using variable

Pass shell environment variable as argument in npm script

Bash pass variable name as argument

How do I pass a string as an argument name?

How to change the name of an environment variable with a different variable?

In Azure Devops or TFS is it possible to pass a PowerShell argument that is variable on the pipeline environment?

Pass variable name as argument inside data.table

How to pass a variable value as keyword argument key?

Serverless — How to pass S3 bucket name as an environment variable to my app

how to pass local variable and argument of same name (using this keyword )inside lambda functions in c++?

How to pass javascript variable as url argument

How to pass a parameter name as an argument to a function

How to pass component name as argument and receive it in function

How to pass the name of a template class to a template argument?

How to pass argument to ENTRYPOINT with variable expansion?

how to pass mutually exclusive argument as a variable

How to pass a variable log as an argument to a script?

How to pass variable argument in meteor find method

How to pass variable object as a value argument

how to pass string variable as globbing argument to command

How to pass a variable as an argument

How to pass a variable as an argument to a method in python with tkinter

How to pass a variable as an argument of another function in python

Pass variable name as argument dynamically on svydesign and dplyr::select functions

How to pass r-value of an environment variable as a CLI argument for an NPM script

How to pass a variable as an argument to a function if the variable exists