D.Zou
D.Zou

Reputation: 798

Why can't I pipe awk outputs into a variable via >

I am trying to pipe awk output into a variable like this:

$ awk -F : '/frost/{print $3}' /etc/group > $mygid
$ echo $mygid
$ 

But when I want to see the mygid variable it just hangs. I had to do it like this:

$ "$(awk -F : '/frost/{print $3}' /etc/group)" 

to get it to work.

I don't understand why.

Thanks

Upvotes: 0

Views: 115

Answers (1)

Mark Reed
Mark Reed

Reputation: 95242

> writes into a file. > $mygld tries to write into a file named whatever is currently in the variable $mygid.

To get output of a command in a variable, you can use command substitution and assignment, or process substitution and read:

mygid=$(awk -F : '/frost/{print $3}' /etc/group)

or

read mygid < <(awk -F : '/frost/{print $3}' /etc/group)

I recommend you read some basic shell programming tutorials, perhaps http://mywiki.wooledge.org/BashGuide

Upvotes: 3

Related Questions