gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:39:15 27 4
gpt4 key购买 nike

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

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

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

我已经尝试了以下各种形式:

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

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

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

建议?

最佳答案

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

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

单引号的使用是主要问题。根据Bash Reference Manual :

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).

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

关于bash - 如何在 Bash 中的引用字符串中使用环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/840536/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com