I have a problem with quoting/escaping in bash that I seem to run into periodically. Typically I find I'm using a command with a couple layers of "s and 's a lot so I decide it would be a good idea to turn it into an alias. At that point everything goes to hell. :) I use perl a lot so I'm particularly susceptable to the version of this problem where I get a command like: perl -e '$bar = "xyzzy"; print "foo\n\t$bar\n"' .. and want to turn it into a bash alias. Unfortunately I can never figure out where to put all the \s, 's, and "s to make this sort of thing work. Adding that next level of quotes to set the alias just causes all sorts of problems for me. Could someone either post a quick example of the syntax for making an alias out of the above perl command, or point me to something like a mini-howto on quoting/escaping in bash? TIA, -John -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/support/faq
On Thu, 28 Sep 2000, John Grant wrote:
I use perl a lot so I'm particularly susceptable to the version of this problem where I get a command like:
perl -e '$bar = "xyzzy"; print "foo\n\t$bar\n"'
Have you tried this: perl -e "\$bar = \"xyzzy\"; print \"foo\\n\\t\$bar\\n\"" Which is just with the entire string in double quotes and with all special characters inside escaped with \. Please note that this will probably NOT run as a normal command, but it should work as an alias. A small test you can always make is to simply echo your quoted string to see what bash will make of it like in the following, I did to check whether my suggestion was the right one. ole@linux:~ > echo "\$bar = \"xyzzy\"; print \"foo\\n\\t\$bar\\n\"" $bar = "xyzzy"; print "foo\n\t$bar\n"
mini-howto on quoting/escaping in bash?
man bash has a good description. Especially, it mentions that if you enclose a string in 's you cannot use the ' character inside the string - not even if you escape it with a \. Regards Ole -- Windows: Where do you want to go today? MacOS: Where do you want to be tomorrow? Linux: Are you coming or what? -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/support/faq
On 29 Sep, Ole Kofoed Hansen wrote:
On Thu, 28 Sep 2000, John Grant wrote:
I use perl a lot so I'm particularly susceptable to the version of this problem where I get a command like:
perl -e '$bar = "xyzzy"; print "foo\n\t$bar\n"'
Have you tried this:
perl -e "\$bar = \"xyzzy\"; print \"foo\\n\\t\$bar\\n\""
< [...] This doesn't answer the real question about quoting and escaping. Ole has the right answer there by suggesting the bash man page, since it is a shell issue - and sometimes a confusing one at that. But narrowly targeting the perl one-liners, you can usually get by without escaping quotes, unless you need a literal one. Multiple pairs of single quotes inside of double quotes are ok, as are multiple pairs of double quotes inside single ones. In those instances where you need nested quotes of the same variety, or just don't to think about it, the quote operators should work nicely. perl -e '$foo=q/bar/;$bar=q/baz/; $baz=qq/$foo/; print qq/$foo $bar $baz\n/; print q/$foo $bar $baz/,qq/\n/;' bar baz bar $foo $bar $baz That doesn't solve all the quoting issues, but does simplify some of them. - Don -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/support/faq
On Fri, Sep 29, 2000 at 10:33:54AM -0400, D E Hammond wrote:
On 29 Sep, Ole Kofoed Hansen wrote:
On Thu, 28 Sep 2000, John Grant wrote:
I use perl a lot so I'm particularly susceptable to the version of this problem where I get a command like:
perl -e '$bar = "xyzzy"; print "foo\n\t$bar\n"'
Have you tried this:
perl -e "\$bar = \"xyzzy\"; print \"foo\\n\\t\$bar\\n\""
< [...]
This doesn't answer the real question about quoting and escaping. Ole has the right answer there by suggesting the bash man page, since it is a shell issue - and sometimes a confusing one at that. But narrowly
Tell me about it. :) Actually not so hard I guess. I just never sat down and figured it out before.
targeting the perl one-liners, you can usually get by without escaping quotes, unless you need a literal one. Multiple pairs of single quotes inside of double quotes are ok, as are multiple pairs of double quotes inside single ones. In those instances where you need nested quotes of the same variety, or just don't to think about it, the quote operators should work nicely.
perl -e '$foo=q/bar/;$bar=q/baz/; $baz=qq/$foo/; print qq/$foo $bar $baz\n/; print q/$foo $bar $baz/,qq/\n/;' bar baz bar $foo $bar $baz
That doesn't solve all the quoting issues, but does simplify some of them.
Yes. I've even done this before when I ran into the problem with a perl one-liner. Another solution I've used for perl-less situations is to create a bash function containing the original command, followed by a "$*". IOW, given: perl -e '$foo=join(" ",@ARGV); print "echo:\n\t$foo\n"' .. in bash you can say, function xyzzy { perl -e '$foo=join(" ",@ARGV); print "echo:\n\t$foo\n"' $* } .. which in most (all?) cases is just like saying, alias xyzzy='perl -e "\$foo=join(\" \",@ARGV); print \"echo:\\n\\t\$foo\\n\""' Putting the command in a function works just like making an alias with it but avoids having to deal with any messy quoting or escaping. This is a good general solution - it would have even worked with the example I gave - but my question was prompted by a desire to understand the quoting/escaping issues with bash. -John -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/support/faq
On Fri, Sep 29, 2000 at 01:31:24PM +0200, Ole Kofoed Hansen wrote:
On Thu, 28 Sep 2000, John Grant wrote:
I use perl a lot so I'm particularly susceptable to the version of this problem where I get a command like:
perl -e '$bar = "xyzzy"; print "foo\n\t$bar\n"'
Have you tried this:
perl -e "\$bar = \"xyzzy\"; print \"foo\\n\\t\$bar\\n\""
Which is just with the entire string in double quotes and with all special characters inside escaped with \.
So basically you have to allow for one level of escapes per nesting of quotes, and stay away from single quotes until the outer levels? Also I'd guess that for each level of double quotes you have to add another layer of "\"s to escape the special characters. Taking this to ridiculous extremes, one could do things like: alias xyzzy='echo "`perl -e \"print \\\"foo\\\\n\\\\tbar\\\\n\\\"\"`"' or even bash -c "bash -c \"bash -c \\\"echo -e \\\\\\\"foo\\\\\\\tbar\\\\\\\"\\\"\"" Whew! This could really give that "\" key a workout, since at each new level you have to escape the escapes. I think this is the thing I didn't really understand. ok, I think I'm beginning to get the hang of this now. :) [...]
mini-howto on quoting/escaping in bash?
man bash has a good description. Especially, it mentions that
I looked at the man page but I don't do well with page after page of text. Give me an example (as you did) or two and I'll get much more out of it.
if you enclose a string in 's you cannot use the ' character inside the string - not even if you escape it with a \.
Yes, that is one of the keys. I did realise this, but my problem was understanding how/what to escape and when so I could turn the 's into "s to allow for the "next level" of quoting. Thanks again for the example. It really helped. -John -- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/support/faq
participants (3)
-
deh@tradersdata.com
-
jmgrant@primenet.com
-
okh-linux@post.cybercity.dk