使用tcsh别名转义双引号

时间:2008-12-20 01:23:10

标签: escaping alias quotes 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。

5 个答案:

答案 0 :(得分:7)

以下所有工作都在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'

括号中的任何内容都在子shell中运行。如果您尝试设置环境变量,这将是不好的,但在其他情况下大多不相关。

最后,这是示例实际执行的内容:

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

答案 1 :(得分:6)

我通过将带有双引号的字符串存储在变量中并使用单引号括起来的字符串来实现它。当我在单引号内使用变量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上用tcsh测试了这个

答案 2 :(得分: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"'"'

或者根本不使用引号并且自由地反复抨击。

The\ man\ said\ \"hello\"

答案 3 :(得分:0)

如果你无法使用别名,只需编写一个简短的shell脚本chmod + x,然后将它放在$ PATH中(如$ HOME / bin):

#!/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中有用吗?

答案 4 :(得分:0)

似乎\"\'无法按预期工作的原因是csh传统上不支持此类语法,因此,如果{{1}无条件地支持它,然后这些较旧的脚本可能无法正常工作。

正如另一个答案所述,tcsh本身具有 tcsh 功能;然而,与上述答案中暗示的不同,这不是一个新功能,它只是一个特定于tcsh的功能,并且实际上至少在27年前被添加到tcsh - 该功能首先记录在手册页中在tcsh.man,v 3.8 1991/07/25 04:50:55,这几乎不符合“新”的要求。

http://mdoc.su/n,f,d/tcsh.1

set backslash_quote
相关问题