From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3441 invoked by alias); 4 May 2016 14:14:35 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 21512 Received: (qmail 22904 invoked from network); 4 May 2016 14:14:33 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.1 X-AuditID: cbfec7f4-f796c6d000001486-6e-572a016a593e Date: Wed, 04 May 2016 15:04:22 +0100 From: Peter Stephenson To: zsh-users@zsh.org Subject: Re: make aliases work inside the function? (using a preprocessor?) Message-id: <20160504150422.7afa6805@pwslap01u.europe.root.pri> In-reply-to: <20160504134632.GA5729@aurora-borealis.phear.org> References: <20160504134632.GA5729@aurora-borealis.phear.org> Organization: Samsung Cambridge Solution Centre X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrHLMWRmVeSWpSXmKPExsVy+t/xy7pZjFrhBte28FnsOLmS0YHRY9XB D0wBjFFcNimpOZllqUX6dglcGUtPTGAq2CRUsfTQLcYGxka+LkZODgkBE4nDb+exQdhiEhfu rQeyuTiEBJYySlzq2MMM4Uxjklj4fhE7SJWQwGlGiY2L3CESZxglbqzqB0uwCKhK/H+8hxXE ZhMwlJi6aTYjiC0iICqxfMVmsBphAW+JbYvamEBsXgF7idX/JwDVs3NwCthJ3MmFGG8rsWXv UrBqfgF9iat/PzFBHGcvMfPKGUaITkGJH5PvsYDYzAJaEpu3NbFC2PISm9e8ZYaYoy5x4+5u 9gmMwrOQtMxC0jILScsCRuZVjKKppckFxUnpuYZ6xYm5xaV56XrJ+bmbGCGh/GUH4+JjVocY BTgYlXh4X3hrhguxJpYVV+YeYpTgYFYS4T31FyjEm5JYWZValB9fVJqTWnyIUZqDRUmcd+6u 9yFCAumJJanZqakFqUUwWSYOTqkGRtG+bRbL1M8VfZCw85L9dob5t/KXMk+B10sSYzdc1gur fxix9iSPpl7xT86rVjl9URsjS20mvZmxT6ecZf+32IOf77+7y6xw5kPusdf7Pub9LD43mesh o5mERHxF8wOtz7pz427Z3vJa8z3g75en71Oaq9LdXz30SVgp/+hK2dO/Gm/auxpWnVBiKc5I NNRiLipOBADFxbNZYQIAAA== On Wed, 4 May 2016 13:46:32 +0000 Marc Chantreux wrote: > sometimes i really need aliases. > sometimes aliases seems to work inside functions. > > sometimes it works, sometimes it doesn't. The probably is likely to be order dependence. If you've defined an alias, it's then available for expansion at the start of the command line *even* if that word is defining a function. This is explained in the FAQ (2.3; this is a long entry, unsurprisingly). pws There is one other serious problem with aliases: consider alias l='/bin/ls -F' l() { /bin/ls -la "$@" | more } `l' in the function definition is in command position and is expanded as an alias, defining `/bin/ls' and `-F' as functions which call `/bin/ls', which gets a bit recursive. This can be avoided if you use `function' to define a function, which doesn't expand aliases. It is possible to argue for extra warnings somewhere in this mess. One workaround for this is to use the "function" keyword instead: alias l='/bin/ls -F' function l { /bin/ls -la "$@" | more } The `l' after `function' is not expanded. Note you don't need the `()' in this case, although it's harmless. You need to be careful if you are defining a function with multiple names; most people don't need to do this, so it's an unusual problem, but in case you do you should be aware that in versions of the shell before 5.1 names after the first were expanded: function a b c { ... } Here, `b' and `c', but not `a', have aliases expanded. This oddity was fixed in version 5.1. The rest of this item assumes you use the (more common, but equivalent) `()' definitions. Bart Schaefer's rule is: Define first those aliases you expect to use in the body of a function, but define the function first if the alias has the same name as the function. If you aware of the problem, you can always escape part or all of the name of the function: 'l'() { /bin/ls -la "$@" | more } Adding the quotes has no effect on the function definition, but suppresses alias expansion for the function name. Hence this is guaranteed to be safe---unless you are in the habit of defining aliases for expressions such as 'l', which is valid, but probably confusing.