问题
I'm trying to run the following commands:
replace -x "must " A2input.txt
replace -x " a" -f -s ## A2input.txt
replace -x to -s ## -a A2input.txt
replace -x faith -f "unequivocal" A2input.txt
And it'd be nice if I could just alias it to something short and simple like "a", "b", "c", "d", etc...
However, some of those arguments have a quote, which is messing up the alias. Does anyone know how to actually escape the double quotes? I've tried things like '\"' and \" but nothing seems to work.
I'm using tcsh as my shell.
回答1:
I got it to work by storing the string with the double quote in a variable with the string surrounded by single quotes. When I use the variable I up inside single quotes.
Example:
[11:~] phi% [11:~] phi% set text = 'a quote "' [11:~] phi% alias ec echo '$text' [11:~] phi% ec a quote " [11:~] phi% [11:~] phi% alias ec echo this has '$text' [11:~] phi% ec this has a quote " [11:~] phi%
I tested this with tcsh on OSX
回答2:
The following all work in tcsh
to accomplish various results:
alias t echo hello world # you may not actually need any quotes alias u 'echo "hello world"' # nested quotes of different types alias v echo\ \"hello\ world\" # escape everything alias w echo '\;'hello'";"' world # quote/escape problem areas only alias x 'echo \"hello world\"' # single quote and escape for literal " alias y "echo "\""hello world"\" # unquote, escaped quote, quote ("\"") alias z 'echo '\''hello world'\' # same goes for single quotes ('\'')
To see how these are interpreted by the shell, run alias
with no arguments:
% alias t (echo hello world) u echo "hello world" v echo "hello world" w (echo \;hello";" world) x echo \"hello world\" y echo "hello world" z echo 'hello world'
Anything in parentheses is run in a subshell. This would be bad if you're trying to set environment variables, but mostly irrelevant otherwise.
Finally, here's what the examples actually do:
% t; u; v; w; x; y; z hello world hello world hello world ;hello; world "hello world" hello world hello world
回答3:
tcsh
has a newer variable backslash_quote
. Not sure when it was added but it is supported in 6.18.01
(version on OS X El Capitan) and 6.19
(latest stable release at time of writing). This makes it possible to escape '
, "
, and `
inside of quotation marks.
set backslash_quote
set sentence = 'I\'m a little teapot.'
set sentence2 = "The man said \"hello\""
If you don't want to use this option, your choices are limited to using a different type of quote around the symbol
"The man said "'"'"hello"'"'
or not using quotes at all and liberally backslashing things.
The\ man\ said\ \"hello\"
回答4:
If you can't get an alias to work, just write a short shell script, chmod +x, and put it somewhere in your $PATH (like $HOME/bin):
#!/bin/tcsh
replace -x "must" ...
I don't have any experience with tcsh, but with bash you do it like any of these:
alias t='echo "hello world"' # using single quotes to enclose entire string
alias t=echo\ \"hello\ \ world\" # escape " and <space>
alias t="echo \"hello world\"" # double-quote + escape inner double quotes
Maybe something similar will work in tcsh?
回答5:
It would appear that the reason that \"
and \'
don't work as you expect is that csh
traditionally didn't support such syntax, so, if tcsh
were to unconditionally support it, then such older scripts may not work properly anymore.
As mentioned in another answer, tcsh itself has the set backslash_quote
feature; however, unlike implied in the mentioned answer, this is hardly a new feature, it's just a tcsh-specific one, and was actually added to tcsh at least some 27 years ago — the feature was first documented in the manual page at tcsh.man,v 3.8 1991/07/25 04:50:55, which hardly qualifies it as "new".
http://mdoc.su/n,f,d/tcsh.1
backslash_quote (+) If set, backslashes (`\') always quote `\', `'', and `"'. This may make complex quoting tasks easier, but it can cause syntax errors in csh(1) scripts.
来源:https://stackoverflow.com/questions/382734/escaping-double-quotes-with-tcsh-alias