Assign output of command to variable in BASH [duplicate] - bash

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?

Related

echo and read with pipe issue [duplicate]

This question already has answers here:
Read values into a shell variable from a pipe
(17 answers)
Why variable values are lost after terminating the loop in bash? [duplicate]
(1 answer)
Closed 17 days ago.
First of all, I am sorry, I am learning the bash and I am a newbie.
Please find the below script.
grep "error" /var/log/syslog | while read line
do
echo $line
done
If I am not wrong,The above script will grep the keyword "error" in /var/log/syslog and will send it inside the while loop as STDIN and output will be displayed.
Also please loop the below script.
echo "hello" | read hi
echo $hi
So when I run this script I am not getting any output, why is that?
should I use any loop? only then I will get output?

What is `>&2` used for? [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)
What does “1>&2” mean in bash? [duplicate]
(1 answer)
Closed 2 years ago.
I am asking in the context of piping the result of the following command to another:
echo "foo" | tee >&2
with >&2 you are sending the output to standard error instead of standard out.
here you can read about the standard I/O streams:
https://man7.org/linux/man-pages/man3/stdin.3.html

How to name a file with current time? [duplicate]

This question already has answers here:
Redirect output to filename given by result of command
(3 answers)
Closed 4 years ago.
I'm trying to create a file using bash that has the current time as it's name. This is how I'm trying to do it:
echo 'hello' > date +"%T".txt
What am I missing?
Use command substitution to capture date's output as a string.
echo 'hello' > "$(date +%T)".txt

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?

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

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)"

Resources