2008-12-20 59 views
3

我试图运行下面的命令:转义双引号tcsh的别名

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 

而且这将会是好的,如果我可以别名它的东西短期和简单的像“A”,“B “,”c“,”d“等...

但是,其中一些参数有一个引号,这是混淆了别名。有谁知道如何避免双引号?我尝试了'\''和\“之类的东西,但似乎没有任何效果。

我使用tcsh作为我的shell。

+0

最后两个不需要引号。用单引号代替双引号怎么样? – 2008-12-20 03:19:39

回答

5

我得到它的工作原理是将双引号字符串存储在变量中,字符串用单引号括起来。当我在单引号内使用变量I时。
例子:

 
[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% 

我在OSX

+0

非常好。我现在在我的别名安装脚本中使用`set q ='“”`。我通常可以弄清楚如何结合``,',\,...`来正确引用,但使用`$ {q}`更具可读性! – 2017-08-21 18:26:49

0

如果你不能得到一个别名工作,只写一个短的shell脚本,使用chmod + x,并把它的地方在你的$ PATH(如$ HOME /箱):

#!/bin/tcsh 
replace -x "must" ... 

我没有与任何tcsh的经验,但在bash你不喜欢它的部分包括:

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 

也许类似的东西会在tcsh的工作吗?

6

与tcsh的测试,这下在tcsh所有的工作来完成各种结果:

 
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 ('\'') 

,以了解这些由shell来解释,无参数运行alias

 
% 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' 

圆括号中的任何内容都在子外壳中运行。如果你试图设置环境变量,这会很糟糕,但是大多数情况下是不相关的。

最后,这里就是例子实际上做:

 
% t; u; v; w; x; y; z 
hello world 
hello world 
hello world 
;hello; world 
"hello world" 
hello world 
hello world 
2

tcsh有一个新的变量backslash_quote。不知道何时添加,但在6.18.01(OS X El Capitan上的版本)和6.19(本文撰写时的最新稳定版本)中受支持。这使得可以在引号内使用',"`

set backslash_quote 

set sentence = 'I\'m a little teapot.' 
set sentence2 = "The man said \"hello\"" 

如果你不想使用此选项,您的选择是有限的使用不同类型的报价在使用所有的报价符号

"The man said "'"'"hello"'"' 

或不在身边和宽松backslashing事情。

The\ man\ said\ \"hello\" 
0

这样看来,那\"\'像您期望的不工作的原因是,传统csh不支持这样的语法,所以,如果tcsh是无条件地支持它,那么这样的老脚本可能无法正常工作了。

作为另一个答复中提到,TCSH本身所具有的set backslash_quote功能;然而,与上述答案中所隐含的不同,这不是一个新功能,它只是一个特定于tcsh的功能,实际上至少在大约27年前被添加到tcsh - 该功能首次记录在手册页tcsh.man,v 3.8 1991/07/25 04:50:55,其中几乎没有资格成为“新”。

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.