Unix Power ToolsUnix Power ToolsSearch this book

29.10. Fix Quoting in csh Aliases with makealias and quote

Getting quoting right in C shell aliases can be a real problem. Dan Bernstein wrote two aliases called makealias and quote that take care of this for you.

For example, here I use makealias to avoid having to quote ! and *:

% makealias mycat
cat `ls | sed '1,/!*/d'` | less
CTRL-d
alias mycat 'cat `ls | sed '\''1,/\!*/d'\''` | less'

I typed the makealias mycat command and the line starting with cat, then pressed CTRL-d and got back an alias definition with all the quoting done correctly.

The properly quoted alias definition is sent to the standard output. That line is what you would use to define the alias.[91]

[91][The mycat alias runs cat on all files with names later in the alphabet than the argument you type. The output of cat is piped to the less (Section 12.3) pager. For example, let's say your current directory has the files afile, count, jim, and report. Typing mycat count would display the files jim and report. -- JP]

Here are the quote and makealias aliases themselves:

Figure Go to http://examples.oreilly.com/upt3 for more information on: makealias.csh

alias quote     "/bin/sed -e 's/\\!/\\\\\!/g' \\
   -e 's/'\\\''/'\\\'\\\\\\\'\\\''/g' \\
   -e 's/^/'\''/' -e 's/"\$"/'\''/'"
alias makealias "quote | /bin/sed 's/^/alias \!:1 /' \!:2*"

Pretty gross, but they do the job. On Darwin, as on many BSD-derived systems, sed is in /usr/bin, not /bin. You may wish simply to use the command name without the explicit path, or use the explicit (but correct) path. On Linux, the script above does not work with tcsh, which handles multi-line aliases anyway.

--JIK and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.