Reading environment variable in command alias causes evaluating it?

1

I have simple alias for writing to log files (this is what I have in my .profile file):

logtee() {
    ${TEST_LOG_FOLDER:?"empty_log_folder"}
    tee -a $TEST_LOG_FOLDER/$1
}

I'm reading the folder from environment variable (so this variable holds value similar to 'd/my/logs'). However, when I try using this alias by executing echo 'this is reading path from env.variable!' | logtee whatever.log it works, but the output is: sh.exe": /d/my/log/path: is a directory.
Did I do something wrong? Why does reading environment variable evaluate it? I suspect it have something to do with a fact that it's an alias, because in usual .sh script similar code works without any issues
P.S. I'm using msysgit on Windows 7, may be that's the problem?

chester89

Posted 2016-08-02T12:11:11.297

Reputation: 149

What do you mean "it's an alias"? I don't see any aliasing here (have you exported the function inside one of your scripts; e.g. ".profile" or ".bashrc", etc.). Also, why do you have "sh.exe" in the output? Are you running on Windows or Linux? And, finally, what is the output of echo $TEST_LOG_FOLDER. – jehad – 2016-08-02T12:17:55.537

@jehad I'm sorry I wasn't clear - updated the question – chester89 – 2016-08-02T13:31:20.610

Answers

3

The first line of your function is indeed evaluating it. You could replace it by:

echo ${TEST_LOG_FOLDER:?"empty_log_folder"} > /dev/null

A better solution would be:

[ -d "${TEST_LOG_FOLDER:?'empty_log_folder'}" ] && \

The best solution is not to use a separate line, so the whole function becomes:-

logtee() {
    tee -a "${TEST_LOG_FOLDER:?'empty_log_folder'}/$1"
}

Note that I have changed the quoting, in case there is a space in $TEST_LOG_FOLDER or $1.

AFH

Posted 2016-08-02T12:11:11.297

Reputation: 15 470