home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 10.7 How to Put if-then-else in a C Shell Alias Chapter 10
Next: 10.9 Shell Functions

10.8 Fix Quoting in csh Aliases with makealias and quote

Getting quoting right in 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


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

I typed the makealias mycat command and the line starting with cat and got back an alias definition with all of 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. [2]

[2] [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 (25.4 ) 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  ]

And here are the quote and makealias aliases themselves:

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.

- JIK in comp.unix.questions on Usenet, 17 February 1991

Previous: 10.7 How to Put if-then-else in a C Shell Alias UNIX Power Tools Next: 10.9 Shell Functions
10.7 How to Put if-then-else in a C Shell Alias Book Index 10.9 Shell Functions

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System