Tod*_*odd 5 apache bash profile alias sed
我有一个别名来查看我的apache错误日志,它使用sed将换行符格式化为换行符以便更好地显示.通过命令行输入时,以下命令完全正常,但不能作为别名.每次运行时,我都需要打开我的.profile,复制别名的内容,然后将其粘贴到命令行中.
tail -f /var/log/httpd/my-sandbox-error_log | sed -e 's/\\n/\n/g'
Run Code Online (Sandbox Code Playgroud)
别名:
alias elog="tail -f /var/log/httpd/my-sandbox-error_log | sed -e 's/\\n/\n/g'"
Run Code Online (Sandbox Code Playgroud)
我已经尝试了很多方法,交换了引用字符,并且很多地尝试了转义字符.似乎我永远不会得到我的别名来使用sed搜索/替换(错误日志是有尾的,没有换行格式).我确定有一些微不足道的遗失,我很天真.我不是unix/linux专家.
任何人都可以告诉我为什么这不作为别名?
如果你做了set -x,echo alias elog=..,alias elog或以其他方式得到的bash可将结果写回到你身边,你会明白为什么它不工作.\\用双引号变成\.
经验法则是,如果你不得不问,你已经超出了别名的用处.改为使用函数:
elog() {
tail -f /var/log/httpd/my-sandbox-error_log | sed -e 's/\\n/\n/g'
}
Run Code Online (Sandbox Code Playgroud)
这样您就不需要任何额外的转义.