Score:0

assign variable from bash mulitline timer output

gn flag

How can I assign a variable to this multiline bash countdown timer output:

seconds=10
    start="$(($(date +%s) + $seconds))"
while [ "$start" -ge `date +%s` ]; do 
    time="$(( $start - `date +%s` ))"
printf '%s\r' "$(date -u -d "@$time" +%S)"
done 

and display the results with an:

echo -e $(timer_output)

syntax all in one line?

such as:

echo -e Script will exit in $(timer_output) seconds.

what I have now:

echo -e "\e[5m\e[1;31mScript will exit in:\e[0m\e[25m"
    seconds=10
    start="$(($(date +%s) + $seconds))"
while [ "$start" -ge `date +%s` ]; do 
    time="$(( $start - `date +%s` ))"
printf '%s\r' "$(date -u -d "@$time" +%S)"
done 

Thank you Terrance for a more elegant solution. Here is how it turned out with some color and it executes cleanly:

seconds=10
for ((i=${seconds};i>=0;i--)); do printf "\r\e[5m\e[1;31mScript will exit in $i seconds...\e[0m\e[25m"; sleep 1; done
Cyrus avatar
cn flag
Please take a look at [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting).
Rolfbruge avatar
gn flag
Thank you Terrance. seconds=10 for ((i=${seconds};i>=0;i--)); do printf "\r\e[5m\e[1;31mScript will exit in $i seconds...\e[0m\e[25m"; sleep 1; done ``` works nicely.
Score:0
id flag

I am not sure what you mean by assign the timer_output variable in a line that is constant. It would be better to use a for loop with the printf and the \r in your line so that it keeps looping and printing the same line over and over until the time as been met.

Example

seconds=10
for ((i=${seconds};i>=0;i--)); do printf "\rScript will exit in $i seconds."; sleep 1; done

enter image description here

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.