使用单引号与在bash回声 [英] Using single quotes with echo in bash

查看:154
本文介绍了使用单引号与在bash回声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的回声和管道运行命令行一些code。
这是code线我试图运行:

I am running some code from the command line by using echo and piping. This is the line of code I am trying to run:

echo 'import Cocoa;println("It's over there")' | xcrun swift -i -v -

我试图用一个反斜杠逃脱,象这样一个反斜杠的单引号:

I tried using a backslash to escape the single quote with a backslash like so:

echo 'import Cocoa;println("It\'s over there")' | xcrun swift -i -v -

这是行不通的。

我已经看到了有关使用bash中单引号的其他问题,但他们似乎有单引号作为脚本的一部分。在我的情况下,单引号是被从回波传递一些字符串的一部分。我真的不知道如何这个特殊的字符串传递到bash的,而不会导致出现以下错误:

That does not work.
I have seen other questions about using single quotes in bash but they seem to have the single quote as part of the script. In my case the single quote is part of some string that is being passed in from the echo. I don't really understand how to pass this particular string into bash without causing the following error:

/bin/sh: -c: line 0: unexpected EOF while looking for matching `"'
/bin/sh: -c: line 1: syntax error: unexpected end of file

这可能是很简单,我只是被愚蠢的,但寻找了一段时间后,我似乎无法弄清楚如何为我的特定情况下做到这一点。

This is probably very simple, and I am just being dumb but after searching for a while I cannot seem to figure out how to do this for my particular situation.

推荐答案

如果您不确定的报价,只是使用的是bash的定界符特点:

If you unsure about the quotes, just use the bash's heredoc feature:

cat <<'SWIFT' | xcrun swift -i -v -
import Cocoa;println("It's over there")
SWIFT

如果你使用它不带引号例如 SWIFT 而不是SWIFT你可以使用bash的内部变量。

And if you use it unquoted e.g. SWIFT instead of 'SWIFT' you can use bash variables inside.

最好的是使用它作为一个功能,如

The best is use it as an function, like

getcode() {
cat <<SWIFT
import Cocoa;println("It's over there $1")
SWIFT
}

getcode "now" | xcrun swift -i -v -

将发送给xcrun文本

will send to xcrun the text

import Cocoa;println("It's over there now")

这篇关于使用单引号与在bash回声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆