0

Before I start I'm not asking for any code to be written just to enlighten and the behavior I am having. I have this snippet of code.

NOW=$(date +"%H")
While [ true ]; do
 echo $NOW
done

I would of expected so that when it would be printed to the screen the time would update since I am storing the date command and formatting it into the variable NOW , but instead all it does is keep printing the same date that the script was started at. Will someone enlighten me on why it does that.

1
  • BTW, while [ true ] isn't doing what you think it is -- the [ ] command is checking the string "true" to see if it's non-blank, and since it is it returns true. But while [ false ], while [ wibble ] etc would all also return true (and hence run the loop forever). It makes much more sense to use while true -- without the [ ], around it true is interpreted as a command, and there is a command named true that always succeeds (i.e. returns true). (Compare with while false, which runs the command false, which always fails/returns false.) Commented Sep 6, 2016 at 3:52

2 Answers 2

3

It behaves as it should. What you expect, could be achieved this way:

while [ true ]; do
    NOW=$(date +"%H")
    echo $NOW
done

Now the variable is updated at each iteration of the loop.

Compare both with this one:

NOW=date

while [ true ]; do
    $NOW
done

Here date is recalculated at each iteration, as the command itself is stored in the variable, not its result.

3
  • Hmm. I thought though since date is not static, that it would of changed every time it was called regardless of where the variable is stored since the i would call $NOW which would call date with the current time. I guess it doesn't pull information like that. Behaves differently than other languages I've used. Thank you for your answer. Commented Sep 5, 2016 at 22:25
  • See the update above. Commented Sep 5, 2016 at 22:33
  • @tomas how would $NOW be used with +"%H" ? Maybe worth updating your example with this. Commented Sep 5, 2016 at 23:13
2

A variable is given a value when it is assigned.

For example

x=1

will set the value of the variable x to 1. The value doesn't change unless you assign a new value to it.

When you do

NOW=$(date +"%H")

then the shell will run the date command and put the results into the NOW variable. Again, the value doesn't change unless you assign a new value. It doesn't matter that date is an external command; it's run once when the variable is assigned the value.

So you either need to assign the value inside the loop... or use a function.

NOW()
{
   date +"%H"
}

This doesn't defined a variable, but a function. You can now use this inside your loop

while [ true ]; do
  echo $(NOW)
done

Note that the way you call it is different.

In this limited case we can make it simpler:

while [ true ]; do
  NOW
done

So the final script would be

#!/bin/bash

NOW()
{
   date +"%H"
}

while [ true ]; do
  NOW
done

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.