如何在 Bash 中的带引号的字符串中使用环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/840536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:09:53  来源:igfitidea点击:

How to use an environment variable inside a quoted string in Bash

bashquotesenvironment

提问by Jamie

I've tried various forms of the following in a bash script:

我在 bash 脚本中尝试了以下各种形式:

#!/bin/bash
svn diff $@ --diff-cmd /usr/bin/diff -x "-y -w -p -W $COLUMNS"

But I can't get the syntax to correctly expand the COLUMNSenvironment variable.

但是我无法获得正确扩展COLUMNS环境变量的语法。

I've tried various forms of the following:

我尝试了以下各种形式:

svn diff $@ --diff-cmd /usr/bin/diff -x '-y -w -p -W $COLUMNS'

and

svn diff $@ --diff-cmd /usr/bin/diff -x '-y -w -p -W ${COLUMNS}'

and

eval svn diff $@ --diff-cmd /usr/bin/diff -x "-y -w -p -W $COLUMNS"

Suggestions?

建议?

采纳答案by TheBonsai

If unsure, you might use the 'cols' request on the terminal, and forget COLUMNS:

如果不确定,您可以在终端上使用 'cols' 请求,而忘记 COLUMNS:

COLS=$(tput cols)

回答by RecursivelyIronic

Just a quick note/summary for any who came here via Google looking for the answer to the general question asked in the title (as I was). Any of the following should work for getting access to shell variables inside quotes:

对于任何通过谷歌来到这里寻找标题中提出的一般问题的答案的人(就像我一样),这只是一个简短的说明/摘要。以下任何一项都适用于访问引号内的 shell 变量:

echo "$VARIABLE"
echo "${VARIABLE}"

Use of single quotes is the main issue. According to the Bash Reference Manual:

单引号的使用是主要问题。根据Bash 参考手册

Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash. [...] Enclosing characters in double quotes (") preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. The characters $and ` retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an !appearing in double quotes is escaped using a backslash. The backslash preceding the !is not removed. The special parameters *and @have special meaning when in double quotes (see Shell Parameter Expansion).

将字符括在单引号 ( ') 中会保留引号内每个字符的字面值。单引号之间不能出现单引号,即使前面有反斜杠。[...] 用双引号 ( ")括起来的字符会保留引号内所有字符的字面值,但$, `, \, 和当启用历史扩展时,!. 字符$和 ` 在双引号内保留其特殊含义(请参阅 Shell 扩展)。反斜杠仅在后跟以下字符之一时保留其特殊含义:$, `, ",\,或换行符。在双引号内,后跟这些字符之一的反斜杠将被删除。没有特殊含义的字符前面的反斜杠保持不变。双引号可以通过在双引号前面加上反斜杠来引用。如果启用,将执行历史扩展,除非!使用反斜杠转义双引号中的出现。前面的反斜杠!不会被删除。双引号中的特殊参数*@具有特殊含义(请参阅Shell Parameter Expansion)。

In the specific case asked in the question, $COLUMNS is a special variable which has nonstandard properties (see lhunath's answer above).

在问题中提出的特定情况下, $COLUMNS 是一个具有非标准属性的特殊变量(参见上面 lhunath 的回答)。

回答by lhunath

Note that COLUMNSis:

注意COLUMNS是:

  1. NOTan environment variable. It is an ordinary bash parameter that is set by bash itself.
  2. Set automatically upon receipt of a SIGWINCHsignal.
  1. 不是环境变量。它是一个普通的 bash 参数,由 bash 本身设置。
  2. 收到SIGWINCH信号后自动设置。

That second point usually means that your COLUMNSvariable will only be set in your interactiveshell, notin a bash script.

第二点通常意味着您的COLUMNS变量只会在您的交互式shell 中设置,而不是在 bash 脚本中。

If your script's stdinis connected to your terminal you can manually look up the width of your terminal by asking your terminal:

如果您的脚本stdin已连接到您的终端,您可以通过询问您的终端来手动查找终端的宽度:

tput cols

And to use this in your SVN command:

并在您的 SVN 命令中使用它:

svn diff "$@" --diff-cmd /usr/bin/diff -x "-y -w -p -W $(tput cols)"

(Note: you should quote"$@"and stay away from eval;-))

(注意:你应该引用"$@"并远离eval;-))

回答by Alex B

The following script works for me for multiple values of $COLUMNS. I wonder if you are not setting COLUMNSprior to this call?

以下脚本适用于我的多个值$COLUMNS。我想知道你是不是COLUMNS在这个电话之前没有设置?

#!/bin/bash
COLUMNS=30
svn diff $@ --diff-cmd /usr/bin/diff -x "-y -w -p -W $COLUMNS"

Can you echo $COLUMNSinside your script to see if it set correctly?

你能$COLUMNS在你的脚本中echo看看它是否设置正确吗?

回答by Colas Nahaboo

You are doing it right, so I guess something else is at fault (not export-ing COLUMNS ?).

你做得对,所以我猜还有其他问题(不是导出 COLUMNS ?)。

A trick to debug these cases is to make a specialized command (a closure for programming language guys). Create a shell script named diff-columns doing:

调试这些情况的一个技巧是创建一个专门的命令(编程语言人员的闭包)。创建一个名为 diff-columns 的 shell 脚本:

exec /usr/bin/diff -x -y -w -p -W "$COLUMNS" "$@"

and just use

只需使用

svn diff "$@" --diff-cmd  diff-columns

This way your code is cleaner to read and more modular (top-down approach), and you can test the diff-columns code thouroughly separately (bottom-up approach).

通过这种方式,您的代码更易于阅读且更模块化(自顶向下方法),并且您可以单独测试差异列代码(自底向上方法)。