Problem with (Understanding) function

I have this code

#!/bin/bash

LZ () {
RETVAL="\n$(date +%Y-%m-%d_%H-%M-%S) --- "
return RETVAL
}

echo -e $LZ"Test"
sleep 3
echo -e $LZ"Test"

which I want to use to make logentrys on my NAS. I expect of this code that there would be output like


2017-03-07_11-00-00 --- Test

2017-03-07_11-00-03 --- Test

But I get (without the three seconds difference)


2017-03-07_11-00-00 --- Test

2017-03-07_11-00-00 --- Test

Who knows the solution of my problem? I thank you for every hint and help.

Hi,

Maybe try something like this ?

#!/bin/bash

Log()
{
        timestamp=`/bin/date +%Y-%m-%d_%H-%M-%S`
        echo "$timestamp" "---" "$*"
        return 0
}

Log "Test"
sleep 3
Log "Test"

So have a "Log" function that exists to return as output any input that's passed to it, but with the timestamp prepended to it, basically.

Hope this helps.

@drysdalk:
Thank you for your answer. Your example works as expected. Unfortunately your suggestion is a bit different from my desired solution :wink:

Beside one centralized script which defines $LZ I have very much scripts which have "log-commands" like

echo -e $LZ"Logentry XY"

I do not want to change every "log-command" in every script. So it would be very helpfull if I would have a function which gives back the log entry (compare to my example).

Hi,

The problem is that (unless I'm missing something here) your syntax, as it stands, isn't valid and so won't work at all.

For example, you have a line like this in your function:

return RETVAL

In Bash, the 'return' function can only return a single numeric value. It should be thought of as the exit value of the function, more or less. You can't return a full alphanumeric string, and so this approach isn't valid. No function in Bash can ever pass back a string by using 'return'.

Secondly, you are using syntax like this in your output:

echo -e $LZ"Test"

Here you're mixing up variables with functions. If you want to call the function 'LZ', you have to do it by essentially treating it as a command name (like 'Log' in my earlier example). Bash will always treat $LZ as the contents of a variable called 'LZ', and not as a function. So the value of it will never change and will always be the same, unless you subsequently change it yourself.

So again unless I'm missing something in your explanation (which I certainly grant I may very well be), then you are going to have to re-write this so that the function is defined correctly, and that your output is generated by calling it correctly.

If you feel there's something I've not grasped here or if you have any other info that might clear things up, then please let me know and I'll be happy to assist further.

I'm afraid that your "desired solution" can't be achieved in the way indicated in your posts. You need to differentiate between shell functions and shell variables. Confusingly, shells can have variables and functions with the same names. The output

2017-03-07_11-00-00 --- Test

is the result of expanding the LZ variable (defined elsewhere, NOT in the code snippet) but NOT calling the LZ () function. That also explains why there's no three second time increase. As functions return exit codes only, this mechanism can't be used to supply deliberate results. You need to print results to stdout, as drysdalk proposed, or capture them in a "command substitution" in the callers code.