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.
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. Butwhile [ false ],while [ wibble ]etc would all also return true (and hence run the loop forever). It makes much more sense to usewhile true-- without the[ ], around ittrueis interpreted as a command, and there is a command namedtruethat always succeeds (i.e. returns true). (Compare withwhile false, which runs the commandfalse, which always fails/returns false.)