Can't set an env variable via pipe

Jun 19, 2008
2
0
If I write a BTM file that does this:
ECHO aaa | ECHO %@line[con,0]
it echos aaa. OK.

If I write a BTM file that does this
SET XXX=aaa
it sets the environment variable XXX as aaa. OK.

If I write a BTM file that does this:
ECHO aaa | SET XXX=%@line[con,0]
it doesn't set the variable xxx.

Why?

I want to set an env variable from the output of a program. In my test I simulated the program with ECHO aaa

Nick
 

rconn

Administrator
Staff member
May 14, 2008
12,404
152
> If I write a BTM file that does this:
> ECHO aaa | ECHO %@line[con,0]
> it echos aaa. OK.
>
> If I write a BTM file that does this
> SET XXX=aaa
> it sets the environment variable XXX as aaa. OK.
>
> If I write a BTM file that does this:
> ECHO aaa | SET XXX=%@line[con,0]
> it doesn't set the variable xxx.
>
> Why?

It *does* set the variable -- but it sets it in the child pipe process, so
it's lost when the pipe exits.

If you're using v10, you can use the new "in-process" pipes; if you're using
v9 or earlier you'll have to save & read the value from a temp file.

Rex Conn
JP Software
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,494
91
Albuquerque, NM
prospero.unm.edu
If you're using v10, you can use the new "in-process" pipes; if you're using v9 or earlier you'll have to save & read the value from a temp file.

If you just need the first (only?) line of output, there is also the handy @EXECSTR function. Think of it as a sort of reversed pipe.

Code:
set xxx=%@execstr[echo aaa]
 
Jun 19, 2008
2
0
Rex: Thanks for the explanation. I have indeed used a temporary file as a work around.

Charles: EXCSTR looks like a good solution. I had forgotten about that - thanks.

Nick
 
May 20, 2008
11,534
103
Syracuse, NY, USA
On Sun, 15 Feb 2009 08:16:37 -0600, rconn <> wrote:


>---Quote---
>> If I write a BTM file that does this:
>> ECHO aaa | SET XXX=%@line[con,0]
>> it doesn't set the variable xxx.
>>
>> Why?
>---End Quote---
>It *does* set the variable -- but it sets it in the child pipe process, so
>it's lost when the pipe exits.
>
>If you're using v10, you can use the new "in-process" pipes; if you're using
>v9 or earlier you'll have to save & read the value from a temp file.

Or use

SET XXX=%@EXECSTR[ECHO aaa]
--
- Vince
 
May 20, 2008
11,534
103
Syracuse, NY, USA
On Sun, 15 Feb 2009 08:16:37 -0600, rconn <> wrote:


>---Quote---
>> If I write a BTM file that does this:
>> ECHO aaa | SET XXX=%@line[con,0]
>> it doesn't set the variable xxx.
>>
>> Why?
>---End Quote---
>It *does* set the variable -- but it sets it in the child pipe process, so
>it's lost when the pipe exits.
>
>If you're using v10, you can use the new "in-process" pipes; if you're using
>v9 or earlier you'll have to save & read the value from a temp file.

Or use

SET XXX=%@EXECSTR[ECHO aaa]
--
- Vince
 
May 20, 2008
3,515
4
Elkridge, MD, USA
Nick Tatham wrote:
| Rex: Thanks for the explanation. I have indeed used a temporary file
| as a work around.
|
| Charles: EXCSTR looks like a good solution. I had forgotten about
| that - thanks.

It's @EXECSTR, and in V10 it is enhanced to allow selecting any line of the
output, not just the first.
--
HTH, Steve
 
May 20, 2008
3,515
4
Elkridge, MD, USA
Nick Tatham wrote:
| Rex: Thanks for the explanation. I have indeed used a temporary file
| as a work around.
|
| Charles: EXCSTR looks like a good solution. I had forgotten about
| that - thanks.

It's @EXECSTR, and in V10 it is enhanced to allow selecting any line of the
output, not just the first.
--
HTH, Steve
 
May 20, 2008
3,515
4
Elkridge, MD, USA
Nick Tatham wrote:
| Rex: Thanks for the explanation. I have indeed used a temporary file
| as a work around.
|
| Charles: EXCSTR looks like a good solution. I had forgotten about
| that - thanks.

It's @EXECSTR, and in V10 it is enhanced to allow selecting any line of the
output, not just the first.
--
HTH, Steve
 
Similar threads
Thread starter Title Forum Replies Date
A Can you set an image file 'tag' with TCC/4NT? Support 2
V Set path permanently from within TCC? Support 3
S Debugger keeps breaking with no break point set. Support 1
Joe Caverly Set a batch variable (%1 - %n) when TCC is executing a batch file Support 5
R How to? Set default application in a special folder Support 4
A How to? Set up SMTP password in TCC-RT Support 1
vefatica WAD More SET /A woes Support 4
vefatica SET /A and multidimensional array. Support 1
M How to set Path variable for non-executable file extensions in 4DOS Support 8
D Need to set Take Command font size when switching to high DPI display Support 0
P "Set" command behavior - a bug? Support 3
M How to? Set the ribbon titles to mixed case Support 1
X How to set %_INIREAD result to variable Support 5
M WAD Set statements in () causes problems Support 8
M How to? How to set a default TITLE for newly opened tab? Support 2
M Fixed Set "VAR=VAL" format causes problems Support 12
vefatica SET /A var=expression=digits Support 3
cxxl Set encoding for batch file Support 4
B TCC 21.01.50 Problem with ALIAS /r and SET /r Support 2
vefatica SET /A var\=...? Support 10
dcantor Documentation SET /T: missing from heading Support 0
fpefpe Documentation Set /v Support 4
vefatica What's SET /Z? Support 1
R Fixed set /r has stopped working in build 35 Support 4
BeechComer Debugger: how to set %1 variable? Support 3
fpefpe How to? Global variable set /v Support 4
fpefpe Documentation Can the title line of tcmd be set? Support 4
J How to set working directory Support 10
Jay Sage Failure of ACTIVATE to Set a Window Position Support 3
rps Fixed Win "set /p" not working in V17 build 53 Support 3
vefatica SET /A "var%=N", TCC vs. CMD Support 0
J Set environment variable from stdout of command Support 3
B How to? Pre-set _tcfolder on startup when Folders is set Auto-Hide Support 6
Dan Glynhampton Fixed @version "force" flag not working, and unable to set separator Support 3
R WAD /= size ranges not set correctly Support 5
S How to? Set the timestamp of ZIP command built archive to match latest member Support 8
Mordachai Help! How to set parameters for script I'm debugging?! Support 6
Roedy SET= Support 2
A How to? Set up a log file for all command output Support 3
M Fixed character set in dir/copy Support 3
Phileosophos Fixed TakeCommand exits upon "set [" command Support 14
R How to set comspec to point to cmd.exe? Support 5
S WAD SET problem - array element initialization Support 11
C Wildcard expansion in FOR set Support 27
vefatica SET question Support 16
T Set 1% then Echo 1% Support 9
S SET /A question; HELP nits Support 8
noahcoad copy /w to FTP does not set time stamp Support 1
M A request for a new feature for the "set" command... Support 5
cgunhouse Accessing variables set using SET /U or SET /S Support 3

Similar threads