开发者

Using awk in BASH alias or function

开发者 https://www.devze.com 2023-04-01 13:17 出处:网络
I\'ve a command that works just fine at the command line, but not when I try to put it in an alias or function.

I've a command that works just fine at the command line, but not when I try to put it in an alias or function.

$ awk '{print $1}' /tmp/textfile
0

That's correct, as '0' is in position 1 of "textfile".

$ alias a="awk '{print $1}' /tmp/textfile"
$ a
1 0 136 94

That's the entire line in "textfile". I've tried every variety of quotes, parentheses and backticks that I could imagine might work. I can get the same problem in a wide variety of formats. 开发者_如何学C

What am I not understanding?


You need to escape the $ like so:

 alias a="awk '{print \$1}' /tmp/textfile"

Otherwise your alias is:

 awk '{print }' /tmp/textfile

Which prints the whole file...


Use a function instead of alias

myfunc(){ awk '{print $1}' file; }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号