How to Redirect Console Output to a Variable

is it possible to redirect console output to a variable?

I believe results <- capture.output(...) is what you need (i.e. using the default file=NULL argument). sink(textConnection("results")); ...; sink() should work as well, but as ?capture.output says, capture.output() is:

Related to ‘sink’ in the same way that ‘with’ is related to ‘attach’.

... which suggests that capture.output() will generally be better since it is more contained (i.e. you don't have to remember to terminate the sink()).

If you want to send the output of multiple statements to a variable you can wrap them in curly brackets {}, but if the block is sufficiently complex it might be better to use sink() (or make your code more modular by wrapping it in functions).

Shell redirect output to variable and console output at same time


var=$(ls -l | tee /dev/stderr)

Note that this is assumed to just be an example -- you should never use ls in scripts, except where the output will only be consumed by human readers.

How do I redirect output to a variable in shell?

Use the $( ... ) construct:

hash=$(genhash --use-ssl -s $IP -p 443 --url $URL | grep MD5 | grep -c $MD5)

Redirecting command output to variable as well as console in bash not working

You have an unnecessary redirect on that tee command. Use:

VAR1=$(ps -u "${USER}" | awk 'NR>1 {print $NF}' | tee /proc/$$/fd/1)

They tee works is that it copies its input to its output, and also to any files whose names you give as arguments. The redirection just messages up with its pass-through behavior.

Something else you could do - since we're not talking about some long-running command here - is first set the variable, then print its value:

VAR1=$(ps -u "${USER}" | awk 'NR>1 {print $NF}' )
echo "$VAR1"

... much simpler :-)

How to store the console output to a variable in R

system has a parameter intern which can be used to save the output to a character vector:

test <- system("pa11y scuolafalconeborsellino.it; perl -e \"print unpack('c', pack('C', $?)), \\$/\"", 
intern = TRUE)

Note that system2 is now prefered and system should be avoided in new code.

How to redirect output from command to variable?

You save the output of a command as a variable like this:

$commandOutput = Restore-ASDatabase -Server $Server -RestoreFile $File -Name $CINPUT -Security:$Choice -AllowOverwrite -ErrorAction Stop

Redirect powershell output and errors to console (in real-time) and to a variable

Some general recommendations up front:

  • Invoke-Expression should generally be avoided, because it can be a security risk and introduces quoting headaches; there are usually better and safer solutions available; best to form a habit of avoiding Invoke-Expression, unless there is no other solution.

  • There is never a reason to use Invoke-Expression to simply execute an external program with arguments, such as ping 127.0.0.1; just invoke it directly - support for such direct invocations is a core feature of any shell, and PowerShell is no exception.

  • If you do need to store a command in a variable or pass it as an argument for later invocation, use script blocks ({ ... }); e.g., instead of $command = 'ping 127.0.0.1', use $command = { ping 127.0.0.1 }, and invoke that script block on demand with either &, the call operator, or ., the dot-sourcing operator.

    When calling external programs, the two operators exhibit the same behavior; when calling PowerShell-native commands, & executes the code in a child scope, whereas . (typically) executes in the caller's current scope.


That Invoke-Expression $command 2>&1 doesn't work as expected looks like a bug (as of PowerShell Core 7.0.0-preview.3) and has been reported in this GitHub issue.

As for a workaround for your problem:

PetSerAl, as countless times before, has provided a solution in a comment on the question:

& { Invoke-Expression $command } 2>&1 | Tee-Object -Variable out_content

{ ... } is a script-block literal that contains the Invoke-Expression call, and it is invoked with &, the call operator, which enables applying stream-redirection expression 2>&1 to the & call, which bypasses the bug.

If $command contained a PowerShell-native command that you wanted to execute directly in the current scope, such as a function definition, you'd use . instead of &.

Redirecting command output to a variable in bash fails

Maybe the output goes to stderr, not stdout? Try this:

OUTPUT=$(sudo apache2ctl configtest 2>&1)


Related Topics



Leave a reply



Submit