Monday, May 09, 2005

Neutralizing metacharacter – a cute trick

First do a thing –type a command like this
$echo * these are ur current items in your current directory

And voilaaa…..urs * will be replaced by all the items in ur directory!!!
That means here “*” sends a different signal to the shell. Otherwise it would have been printed like
“* these are ur current items in your current directory”.
Now the question is how to neutralize this effect. We use “\” or “ ’ ” for that.
Say u wanna echo this –“*?*”
See how to do that--
$ echo *?*
bckup cal cuckoo dir lerndollar mkex ren sh2.sh sh3.sh splls welcome

$ echo \*\?\*
*?*

$ echo '*?*'
*?*
$

That means here that back slash and the quote neutralize the metacharacters.

Another point here sometimes while transferring folder or files from windows to Unix we face a great problem with those transferred items in UNIX as they have longgg file or directory names. Then always treat them with simple quote (‘) like
Cat ‘My Documents’ etc
Now lets make the things quite tough. Till now we know bout only two escapes. Simple quotes and backslash. But there is the big brother also .its double quotes. So the common question comes bout their performances. Ok…. we can say the backslash is the most soft among them. Then comes simple quotes n then double quotes. Lets see an example and then we’ll discuss further….

$ echo `ls`
bckup cal cuckoo dir lerndollar mkex ren sh2.sh sh3.sh splls welcome

( remember the usage of back quotes ---if u cant see go to http://mahasim.blogspot.com/2005/05/howto-shell-scripts-dollar.html )

$ echo '`ls` ls is treated as a $ command'
`ls` ls is treated as a $ command

$ echo "`ls` hi hello"
bckup
cal
cuckoo
dir
lerndollar
mkex
ren
sh2.sh
sh3.sh
splls
welcome hi hello

So the learning is that backslash demolish the meaning of the character after that. Simple quote demolishes the in-between characters .And double quote turns off all metacharacters except $,`,and \"

Labels:

0 Comments:

Post a Comment

<< Home