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"
Related
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?
This question already has answers here:
YYYY-MM-DD format date in shell script
(17 answers)
Closed 2 years ago.
I would like to use date in the following format: yyyyMM.dd.HHmm. i.e. 202008.15.1742
This is the Bash file:
#!/usr/bin/env bash
VERSION_CODE=$((($(date +%s%N)/1000000))
plutil -replace CFBundleVersion -string "$VERSION_CODE"
How do I set VERSION_CODE such that the current time appears in this format: yyyyMM.dd.HHmm?
A pure Bash solution without spawning a sub-process:
printf -v VERSION_CODE '%(%Y%m.%d.%H%M)T'
You can do it like this:
VERSION_CODE=$(date +"%Y%m.%d.%H%M")
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
How do i store the output of a bash command in a variable? [duplicate]
(2 answers)
Closed 3 years ago.
I'm trying to get the number of lines that was printed from the ps command, and save to a variable.
COUNT_PS= ps -C $NAME | wc -l)
the line above prints me 2, but COUNT_PS unfortunately still equals 0 (at the start of the script I've assigned COUNT_PS as 0).
It actually prints the value - but doesn't save it to COUNT_PS.
What am I doing wrong?
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
This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I am trying to write a shell script that uses wget to download files in bulk from urls that follow a certain numeric pattern.
Understandably, the url from the user input must contain the variable $i.
dl.sh http://some/url/$i/some/url 1 9
This yields repeated result from the final loop because $i will be expanded before passing down into the loop.
http://some/url/9/some/url
http://some/url/9/some/url
...
http://some/url/9/some/url
Is there a workaround to get this shell script working?
Source Code:
#!/bin/bash
# dl.sh url | index_from | index_to
for i in $(seq $2 $3)
do
echo ${1} # replace with wget for actual download.
done
Expected Result:
http://some/url/1/some/url
http://some/url/2/some/url
http://some/url/3/some/url
...
http://some/url/9/some/url