I've solved a problem with a larger script I'm producing. Basically I have a function that returns the current date and time in a specific format. I need every time the variable data_hora
is printed on the screen, its values are updated from the return of execution of that function.
UPDATE
[12/28/2015]
It is important to note that the variable must be interpolated into a string. In case, every time we print% w / o the output should be the updated date.
This code is used in a script that is loaded only once (it composes my .dotfiles). Every time this variable is interpolated into a string it has returned to the date / time the font of the script was loaded and not the current time.
For example, we print the variable 3x at intervals of 1 second.
The desired output is as follows:
[27/12/2015 22:26:00]
[27/12/2015 22:26:01]
[27/12/2015 22:26:02]
However, the sample code below outputs the following:
[27/12/2015 22:26:00]
[27/12/2015 22:26:00]
[27/12/2015 22:26:00]
Below the source code:
function data_hora_atual {
echo 'date +"[%d/%m/%Y %H:%M:%S]"'
}
obter_data=$(data_hora_atual)
data_hora=$(echo "${obter_data}")
echo ${data_hora}
sleep 1
echo ${data_hora}
sleep 1
echo ${data_hora}