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]
Here are the quote and
makealias aliases themselves:
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
| | | 29.9. How to Put if-then-else in a C-Shell Alias | | 29.11. Shell Function Basics |
Copyright © 2003 O'Reilly & Associates. All rights reserved.
|