PDA

View Full Version : tcsh and quotes


jmb
01-26-2002, 04:31 PM
Hi,

I'm adding custom aliases to my .tcshrc file, and I have a question... I got a sample file from a friend, and some of the alias stings are set off with single quotes (') and some with double quotes (")... For example:

alias blah 'blah blah blah'

or

alias blab "blabbity blabbity"

He said there was a reason for using one over the other under different circumstances, but didn't know why... So, what's the answer? It does seem to make a difference in certain cases, but not in others. For example, my alias for opening the system prefs is as follows:

alias prefs "open -a /Applications/System\ Preferences.app"

I had previously tried it with single quotes, but I couldn't get it to 'source.' When I typed:

source .tcshrc

It gave me:

Unmatched '.

But when I comment it out or use double quotes, all is fine. Other alises for starting apps don't behave this way. Why?

Thanks,

--jmb

Novajo
01-26-2002, 07:20 PM
With double quotes, variables are interpolated (replaced with their values). With single quotes, they are not.

If you define

alias test1 "echo $youwin$"

This will fail telling you that $youwin is not a variable because it is enclosed in double quotes. It might be what you want to say though: you might really want to define echo $youwin$. You have to define it this way:

alias test1 'echo $youwin$'

so that anything prefixed by a dollars $ is not mistaken for a variable.

mervTormel
01-29-2002, 04:08 PM
navajo, thanks for the great explanation.

so

single quotes are always a literal string of characters, passed thru the shell processing unaltered.

&

double quotes are a literal string (?) that the shell will scan and interpolate shelly things?

there's more to it than that, i believe, because there are other shell quoting chars, but that's a starter.

thatch
10-25-2002, 01:43 AM
I'm looking for a little clarification on special quoting circumstances, specifically this alias I would like to create although the single quote doesn't work and neither does double quote or back tics:

alias frsrc 'find . -type f | sed 's/^/"/;s/$/\/rsrc"/' | xargs ls -l'

This is in a tcsh shell with Jag 10.2.1 and will find resource fork files in the cwd. Does anyone know if there is another way to get it working without changing the guts of the line too much?

Muchly appreciated in advance.

ericw13
10-25-2002, 07:43 AM
Originally posted by thatch
I'm looking for a little clarification on special quoting circumstances, specifically this alias I would like to create although the single quote doesn't work and neither does double quote or back tics:

alias frsrc 'find . -type f | sed 's/^/"/;s/$/\/rsrc"/' | xargs ls -l'

This is in a tcsh shell with Jag 10.2.1 and will find resource fork files in the cwd. Does anyone know if there is another way to get it working without changing the guts of the line too much?

Muchly appreciated in advance.

The problem is the quotes for sed. As soon as you hit the ' after sed, you've closed the quote that started just before find, leaving

find . -type f | sed

not an overly useful command.

Any time you need to use literal ' (and possibly the double quotes ") inside a quoted alias, backslash escape them (' -> \', " -> \")...

alias frsrc 'find . -type f | sed \'s/^/\"/;s/$/\/rsrc\"/\' | xargs ls -l'

Again, you may or may not need to escape the double quotes so try it both ways.

HTH
Eric

ericw13
10-25-2002, 07:45 AM
Sheesh... what is with this board and disappearing backslashes?!?! Let's try this again (and learn to use preview more often :) )

alias frsrc 'find . -type f | sed \'s/^/\"/;s/$/\/rsrc\"/\' | xargs ls -l'

Again, you may or may not need to escape the double quotes...

Eric

thatch
10-26-2002, 02:54 AM
ericw13, thanks for the reply. What you said makes perfect sense and I had tried the escapes to no avail both ways. I'm still getting the illegal variable name message from the shell. Can you suggest anything else? TIA.

Just to be clearer, here are what I've tried...


alias frsrc 'find . -type f | sed \'s/^/"/;s/$/\/rsrc"/\' | xargs ls -l'

alias frsrc 'find . -type f | sed \'s/^/\"/;s/$/\/rsrc\"/\' | xargs ls -l'


alias frsrc "find . -type f | sed 's/^/\"/;s/$/\/rsrc\"/' | xargs ls -l"

alias frsrc "find . -type f | sed \'s/^/\"/;s/$/\/rsrc\"/\' | xargs ls -l"

:confused:

ericw13
10-28-2002, 08:31 AM
I'm not much up on sed (preferring to use perl for pattern matching...), but this is what I used to create the alias (this is bash, so slightly different syntax for setting aliases):

alias frsrc='find . -type f | sed "s/^/\"/;s/$/\/rsrc\"/" | xargs ls -l'

Not entirely sure what the sed part actually does though. From your original post, you said you want to limit the search to cwd. If so, you should add -maxdepth 0 to the find command. This will prevent find from traversing down the directory structure.

Note the differences: the sed commands are delimited with ", not '. The literal " inside sed are backslash escaped.

HTH
Eric

thatch
10-28-2002, 04:12 PM
ericw13, thanks again for your reply. I'm chalking this one up to the wonkiness of tcsh. It appears that other shells work fine as you have shown.

Originally posted by ericw13
Not entirely sure what the sed part actually does though. From your original post, you said you want to limit the search to cwd....

Actually, my original post just stated that the line finds resource files in the cwd, which it does do quite nicely without the aliasing in tcsh. The sed line just filters down any filename beginning in anything and ending in rsrc and pipes it on to xargs.

mervTormel
10-28-2002, 05:07 PM
another ding against the C-shell family...

% man csh
...
After the input line is aliased and parsed, and before each command is
executed, variable substitution is performed keyed by `$' characters.
This expansion can be prevented by preceding the `$' with a `\\' except
within `"'s where it always occurs, and within `''s where it never
occurs. Strings quoted by ``' are interpreted later (see Command
substitution below) so `$' substitution does not occur there until later,
if at all. A `$' is passed unchanged if followed by a blank, tab, or
end-of-line.
...


from UNIX Power Tools:
...

you can't quote things reasonably well in csh.

dollar signs ($) cannot be escaped in double quotes in csh. ugh

% set foo = "this is a \$dollar quoted and this is $HOME not quoted"
dollar: Undefined variable.

...

thatch, i suggest you craft that find into a sh script.

for filtering out the chaff, i've had luck with this incantation...

$ find . -type f | sed 's/^/"/;s/$/\/rsrc"/' | xargs ls -l | \
awk '{if ($5 > 0 ) print $0 }'

thatch
10-29-2002, 01:09 AM
mT, thanks. The sh script in ~/bin with the name frsrc works excellent. And the addition of the awk line leaves out all the zero byte files. Perfect!

:)