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

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?

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?

How to redirect output from commands to a file in bash? [duplicate]

This question already has answers here:
Redirect all output to file in Bash [duplicate]
(9 answers)
Closed 2 years ago.
I was wondering how to redirect the output from my bash file (which is getting its input from another redirect) to a txt file.
This is how I run my program:
$ ./myFile.bash < input.txt
I would like to save my output to some txt file (something like this):
$ out.txt < ( myFile.bash < input.txt )
What would be the most correct/formal/generic ways to go about this? Are there multiple options if any?
$ ./myFile.bash < input.txt > out.txt

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

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

Resources