How to save command output to variable

Viewed 558

i'm writing a script in expect and want to save the output of an command in a variable. Is that possible? The command is

echo "text here"| base64

i know i can set a variable with "set" but

set var ["echo text here| base64"]

or

set var [ spawn "echo text here| base64"]

doesn't work. With that i'm just saving the string not the output.

1 Answers

The exec command returns the output from a command. That lets you set it into a variable:

set var [exec echo "text here" | base64]

However, you can avoid the echo:

set var [exec base64 << "text here"]

And in Tcl 8.6 you can completely skip running an external program:

set var [binary encode base64 "text here"]

There's also a base64 encoder in Tcllib:

package require base64
set var [base64::encode "text here"]
Related