Bash how do you capture stderr to a variable? [duplicate] - bash

This question already has answers here:
Bash script - store stderr in a variable [duplicate]
(4 answers)
Closed 6 years ago.
Bash how do you capture stderr to a variable?
I would like to do something like this inside of my bash script
sh -c path/myExcecutable-bin 2>&1 =MYVARIABLE
How do you send stderror output to a variable ?

To save both stdout and stderr to a variable:
MYVARIABLE="$(path/myExcecutable-bin 2>&1)"
Note that this interleaves stdout and stderr into the same variable.
To save just stderr to a variable:
MYVARIABLE="$(path/myExcecutable-bin 2>&1 > /dev/null)"

Related

Redirect stderr to stdout without also redirecting to a file? [duplicate]

This question already has answers here:
What does " 2>&1 " mean?
(19 answers)
Closed 5 months ago.
Is there a way to redirect stderr to stdout, so that all stderr output is received in stdout, but without also redirecting to a file?
There are of course a number of ways of redirecting stderr to stdout and then outputting the combined stream to a file, but in this case I want all of the output to just come through stdout without any files being involved.
As I understand, you want to redirect stderr to stdout and print stdout to terminal?
If, so:
command 2>&1
where:
0 = stdin
1 = stdout
2 = stderr
2>&1 means: redirect stderr into stdout
you can also do:
2>filename

Assign output of command to variable in BASH [duplicate]

This question already has an answer here:
Bash how do you capture stderr to a variable? [duplicate]
(1 answer)
Closed 3 years ago.
I would like to assign output of a specific command to variable (nginx -v). My trouble can be seen at printscreen bellow, it still prints output to stdout.
Thank you for your help
a=$(nginx -v 2>&1)
echo $a
Bash how do you capture stderr to a variable?

How to echo and save at the same time? [duplicate]

This question already has answers here:
echo to stderr and tee to log file?
(4 answers)
How to redirect output to a file and stdout
(11 answers)
Writing outputs to log file and console
(9 answers)
Closed 5 years ago.
Is there an elegant way to both echo a command and save it to a file? The common use case is echo "test" >> someappendedfile.txt, in which case the command does not appear in bin/bash. By definition, >> is a re-direction, so doing both seems like a weird command.
I would like the result of something like echo "test" >> someappendedfile.txt to both log to stdout and the appending file.
Is there an alternative operator or utility that does what I am asking?

How to combine these two commands?: ./script.sh > logfile.log and ./script.sh 2> logfile.log [duplicate]

This question already has answers here:
How to redirect and append both standard output and standard error to a file with Bash
(8 answers)
Closed 6 years ago.
How to combine these two commands?
./script.sh > logfile.log
and
./script.sh 2> logfile.log
Thank you!
In bash, to redirect both standard output and standard error to the same file, you can write
./script.sh &> logfile.log
If you want to be compatible with other shells,
./script.sh > logfile.log 2>&1

Unix: Is it possible to copy error output to a file while still displaying all output? [duplicate]

This question already has answers here:
How to redirect output to a file and stdout
(11 answers)
Closed 7 years ago.
I've seen things like
command > out.txt
Sometimes appended with
2&1
But these seem to divert all output or all error streams instead of both displaying and copying the output into the file, which is what I'm trying to do. Do I have any options?
If you can have stdout and stderr into the file
command 2>&1 | tee -a log
Else you swap stdout and stderr using 3
{ command | tee stdout.log; } 3>&1 1>&2 2>&3 | tee stderr.log

Resources