Is it possible to set a statement\'s output of a batch file to a variable, for example:
findstr testing > %VARIABLE%
echo %VARIABLE%
These answers were all so close to the answer that I needed. This is an attempt to expand on them.
If you're running from within a .bat
file and you want a single line that allows you to export a complicated command like jq -r ".Credentials.AccessKeyId" c:\temp\mfa-getCreds.json
to a variable named AWS_ACCESS_KEY
then you want this:
FOR /F "tokens=* USEBACKQ" %%g IN (`jq -r ".Credentials.AccessKeyId" c:\temp\mfa-getCreds.json`) do (SET "AWS_ACCESS_KEY=%%g")
If you're at the C:\
prompt you want a single line that allows you to run a complicated command like jq -r ".Credentials.AccessKeyId" c:\temp\mfa-getCreds.json
to a variable named AWS_ACCESS_KEY
then you want this:
FOR /F "tokens=* USEBACKQ" %g IN (`jq -r ".Credentials.AccessKeyId" c:\temp\mfa-getCreds.json`) do (SET "AWS_ACCESS_KEY=%g")
The only difference between the two answers above is that on the command line, you use a single % in your variable. In a batch file, you have to double up on the percentage signs (%%).
Since the command includes colons, quotes, and parentheses, you need to include the USEBACKQ
line in the options so that you can use backquotes to specify the command to run and then all kinds of funny characters inside of it.