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

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

Related

Get current time (and date) WITHOUT opening a subshell [duplicate]

This question already has answers here:
Getting the current date in bash without spawning a sub-process
(2 answers)
YYYY-MM-DD format date in shell script
(17 answers)
Closed 2 years ago.
Is it possible to get current time ( and possibly date ) without doing it via a subshell?
because if I'm not mistaken, this command do open a subshell?
d=$(date)
With Bash≥4.2 you can use printf with the %(datefmt)T format:
printf '%(%c)T\n' -1
-1 means now.
See The Bash reference at the printf entry.
To put it in a variable (and hence not use a subshell):
printf -v d '%(%c)T' -1
echo "$d"

Store into var the result of grep command shell script [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 3 years ago.
I'am creating a shell script to extract a number from a particular line, where one particular string appears (isDone), for that i use a grep, i find the line and can echo it, but i can't store the grep output to a var.
$text var got inumerous tags, one of them having the string "isDone", thats the line i want:
code:
short_str="isDone"
echo "$text" | grep "$short_str"
output:
< s:key name="isDone">1</s:key >
now i want to store that output from grep into a file, and then extract the value (on this case is 1)
what have i tried:
store="$("$text" | grep "$short_str")"
echo "$store"
but that outputs all the file, what am i doing wrong?

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 search for a pattern in a file and store it in a variable using shell script? Please give an example [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Bash script store command output into variable
(2 answers)
Save output in variable? [duplicate]
(2 answers)
Store output of sed into a variable [duplicate]
(4 answers)
Assigning the output of a command to a variable [duplicate]
(3 answers)
Closed 4 years ago.
suppose in a file if I have " Route : 8888" , I want to get the string after the pattern "Route :"
I have used sed command, but it is printing 8888 but I want to store it into a variable and use for further processing
So store sed's output in a variable:
ROUTE_VALUE=$(sed ... <file.txt)
Where '...' is your sed command.
$ cat rt.sh
ROUTE=`sed -n 's/.*Route = \(.*\),.*/\1/p' data.txt`
echo $ROUTE
$ bash rt.sh
8888
$ source rt.sh
8888

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?

Resources