Corn-fuzed
Corn-fuzed

Reputation: 331

Using awk in BASH 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.

What am I not understanding?

Upvotes: 33

Views: 18700

Answers (2)

ghostdog74
ghostdog74

Reputation: 342423

Use a function instead of alias

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

Upvotes: 18

Linus Kleen
Linus Kleen

Reputation: 34632

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...

Upvotes: 70

Related Questions