Setting up an alias for 'pkg info | awk'

Mike Clarke jmc-freebsd2 at milibyte.co.uk
Sat Jan 17 14:56:24 UTC 2015


On Saturday 17 Jan 2015 08:18:10 zep wrote:
> > I am trying to list out all the ports on my system with a Bash shell
> > alias :
> > 
> > alias pia="pkg info | awk '{print $1}'"
> > 
> > But this alias simply prints the output of 'pkg info'.
> > 
> > Is there some way to do this ?
> 
> it doesn't make any sense to me why the alias wouldn't be working,

$1 is in a string quoted with double quotes so gets evaluated (to a null 
string) when the alias is defined so you end up with {print } as the awk 
command:

$ alias pia="pkg info | awk '{print $1}'"
$ alias pia
pia='pkg info | awk '\''{print }'\'

Simpler solutions not requiring awk have been given elsewhere in this thread 
but the awk command can be made to work by using Dollar-Single Quotes to quote 
the $ character:

$ alias pia="pkg info | awk '{print $'$'1}'"
$ alias pia
pia='pkg info | awk '\''{print $'\''$'\''1}'\'

$ pia | head -3
GentiumBasic-110_1
GeoIP-1.6.4
ImageMagick-6.9.0.2,1

An alternative would be to break the alias string down into 3 concatenated 
strings which is a bit more error prone to type but makes the resulting alias 
look a bit less like Klingon poetry:

$ alias pia="pkg info | awk '{print "'$1'"}'"
$ alias pia
pia='pkg info | awk '\''{print $1}'\'
$ pia | head -3
GentiumBasic-110_1
GeoIP-1.6.4
ImageMagick-6.9.0.2,1


-- 
Mike Clarke


More information about the freebsd-questions mailing list