From owner-freebsd-questions@FreeBSD.ORG Sat Jan 17 14:56:24 2015 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id ACDB04BF for ; Sat, 17 Jan 2015 14:56:24 +0000 (UTC) Received: from avasout07.plus.net (avasout07.plus.net [84.93.230.235]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (Client CN "Bizanga Labs SMTP Client Certificate", Issuer "Bizanga Labs CA" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4047CCF for ; Sat, 17 Jan 2015 14:56:23 +0000 (UTC) Received: from curlew.milibyte.co.uk ([84.92.153.232]) by avasout07 with smtp id h2wE1p005516WCc012wFFv; Sat, 17 Jan 2015 14:56:15 +0000 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=Y852s3uN c=1 sm=1 tr=0 a=lfSX4pPLp9EkufIcToJk/A==:117 a=lfSX4pPLp9EkufIcToJk/A==:17 a=D7rCoLxHAAAA:8 a=0Bzu9jTXAAAA:8 a=cYTjFNdXV1EA:10 a=kj9zAlcOel0A:10 a=YNv0rlydsVwA:10 a=1vkJWZk8uRLtmz4b-a4A:9 a=CjuIK1q_8ugA:10 Received: from curlew.lan ([192.168.1.13] helo=milibyte.co.uk) by curlew.milibyte.co.uk with esmtp (Exim 4.85) (envelope-from ) id 1YCUnN-0001gb-Rh; Sat, 17 Jan 2015 14:56:14 +0000 From: Mike Clarke To: freebsd-questions@freebsd.org Date: Sat, 17 Jan 2015 14:56:13 +0000 Message-ID: <2057440.qDmOUYx2b0@curlew.lan> User-Agent: KMail/4.14.2 (FreeBSD/10.1-RELEASE; KDE/4.14.2; amd64; ; ) In-Reply-To: <54BA6112.1070406@gmail.com> References: <54BA6112.1070406@gmail.com> MIME-Version: 1.0 X-SA-Exim-Connect-IP: 192.168.1.13 X-SA-Exim-Mail-From: jmc-freebsd2@milibyte.co.uk X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on curlew.lan X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=ALL_TRUSTED,BAYES_00 autolearn=ham autolearn_force=no version=3.4.0 Subject: Re: Setting up an alias for 'pkg info | awk' Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-SA-Exim-Version: 4.2 X-SA-Exim-Scanned: Yes (on curlew.milibyte.co.uk) Cc: zep , Manish Jain X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Jan 2015 14:56:24 -0000 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