使用 tcsh 别名转义双引号

发布于 2024-07-10 06:05:31 字数 362 浏览 5 评论 0原文

我正在尝试运行以下命令:

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。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

转瞬即逝 2024-07-17 06:05:31

以下所有内容在 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

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
明月松间行 2024-07-17 06:05:31

我通过将带有双引号的字符串存储在变量中并用单引号括起来的字符串来使其工作。 当我使用变量时,我将其放在单引号内。
示例:

[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 对此进行了测试

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

友欢 2024-07-17 06:05:31

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\"

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\"
云裳 2024-07-17 06:05:31

看来 \"\' 不能按您预期工作的原因是 csh 传统上不支持此类语法,所以,如果 tcsh 无条件支持它,那么这些旧的脚本可能不再正常工作,

正如另一个答案中提到的,tcsh 本身有 set backslash_quote<。 /code>功能;但是,与上述答案中暗示的不同,这并不是一个新功能,它只是 tcsh 特定的功能,并且实际上已添加到 tcsh 至少大约 27 年前 — 该功能首次记录在 tcsh.man,v 3.8 1991/07/25 04:50:55,这几乎不合格作为“新”

。 .1

反斜杠引号 (+) 
         如果设置,反斜杠 (`\') 总是引用 `\'、`'' 和 `"'。 
         可能会使复杂的引用任务变得更容易,但它可能会导致语法问题 
         csh(1) 脚本中的错误。 
  

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.
纵山崖 2024-07-17 06:05:31

如果您无法使用别名,只需编写一个简短的 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 中工作?

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?

你在我安 2024-07-17 06:05:31

在双引号中转义双引号 - 将双引号括起来以用单引号括起来的双引号进行转义 - '"'"'"'

如果您能理解以下命令,那么您会得到它:

set var = "`echo '"'"'"'A'"'"'"''"'"'"'B C '"'"'"'`" ; echo $var

escape double quote in double quote - surround the double quote to escape with double quotes which are surrounded by single quote - '"'"'"'

if you can understand below commands, then you get it:

set var = "`echo '"'"'"'A'"'"'"''"'"'"'B C '"'"'"'`" ; echo $var
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文