Question

Ive got an alias to view my apache error log, which uses sed to format newline characters into newlines for better display. The following command works perfectly fine when entered via command line, but wont work as an alias. Every time I run this, I need to open my .profile, copy the contents of the alias, and paste it in command line.

tail -f /var/log/httpd/my-sandbox-error_log | sed -e 's/\\n/\n/g'

alias:

alias elog="tail -f /var/log/httpd/my-sandbox-error_log | sed -e 's/\\n/\n/g'"

Ive tried a number of approaches, have swapped the quotation characters, and experimented with escape characters a'plenty. It seems I can never get my alias to make use of the sed search/replace (error log is tailed, with no newline formatting). Im sure there is something trivial missing, to which I am naive. I am not a unix/linux expert.

Can anyone enlighten me as to why this does not work as an alias?

Was it helpful?

Solution

If you did set -x, echo alias elog=.., alias elog or otherwise got bash to write the result back to you, you'd see why it isn't working. \\ in double quotes becomes \.

The rule of thumb is that if you have to ask, you've exceeded the usefulness of an alias. Use a function instead:

elog() {
    tail -f /var/log/httpd/my-sandbox-error_log | sed -e 's/\\n/\n/g'
}

This way you don't need any additional escaping.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top