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 COLUMNS
environment 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?
5 个解决方案
#1
If unsure, you might use the 'cols' request on the terminal, and forget COLUMNS:
如果不确定,您可以在终端上使用'cols'请求,并忘记COLUMNS:
COLS=$(tput cols)
#2
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).用单引号(')括起字符可以保留引号中每个字符的字面值。单引号之间可能不会出现单引号,即使前面有反斜杠也是如此。 [...]用双引号括起来的字符(“)保留引号内所有字符的字面值,但$,`,\除外,并且,当启用历史记录扩展时,!。字符$和`retain它们在双引号内的特殊含义(参见Shell Expansions)。反斜杠只有在跟随以下字符之一时才保留其特殊含义:$,`,“,\或newline。在双引号内,将删除后跟其中一个字符的反斜杠。没有特殊含义的字符前面的反斜杠保持不变。双引号可以在双引号内引用,前面加一个反斜杠。如果启用,将执行历史记录扩展,除非!出现在双引号中的是使用反斜杠转义的。之前的反斜杠!没有删除。在双引号中,特殊参数*和@具有特殊含义(请参见壳参数扩展)。
In the specific case asked in the question, $COLUMNS is a special variable which has nonstandard properties (see lhunath's answer above).
在问题中提到的特定情况下,$ COLUMNS是一个具有非标准属性的特殊变量(参见上面的lhunath答案)。
#3
Note that COLUMNS
is:
请注意,COLUMNS是:
- NOT an environment variable. It is an ordinary bash parameter that is set by bash itself.
- Set automatically upon receipt of a
SIGWINCH
signal.
不是环境变量。它是一个普通的bash参数,由bash本身设置。
收到SIGWINCH信号后自动设置。
That second point usually means that your COLUMNS
variable will only be set in your interactive shell, not in a bash script.
第二点通常意味着您的COLUMNS变量将仅在交互式shell中设置,而不是在bash脚本中设置。
If your script's stdin
is 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 ;-))
#4
The following script works for me for multiple values of $COLUMNS
. I wonder if you are not setting COLUMNS
prior 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 $COLUMNS
inside your script to see if it set correctly?
你可以在脚本中回显$ COLUMNS以查看它是否设置正确吗?
#5
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).
通过这种方式,您的代码更易于阅读并且更加模块化(自上而下的方法),您可以单独测试diff-columns代码(自下而上的方法)。
#1
If unsure, you might use the 'cols' request on the terminal, and forget COLUMNS:
如果不确定,您可以在终端上使用'cols'请求,并忘记COLUMNS:
COLS=$(tput cols)
#2
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).用单引号(')括起字符可以保留引号中每个字符的字面值。单引号之间可能不会出现单引号,即使前面有反斜杠也是如此。 [...]用双引号括起来的字符(“)保留引号内所有字符的字面值,但$,`,\除外,并且,当启用历史记录扩展时,!。字符$和`retain它们在双引号内的特殊含义(参见Shell Expansions)。反斜杠只有在跟随以下字符之一时才保留其特殊含义:$,`,“,\或newline。在双引号内,将删除后跟其中一个字符的反斜杠。没有特殊含义的字符前面的反斜杠保持不变。双引号可以在双引号内引用,前面加一个反斜杠。如果启用,将执行历史记录扩展,除非!出现在双引号中的是使用反斜杠转义的。之前的反斜杠!没有删除。在双引号中,特殊参数*和@具有特殊含义(请参见壳参数扩展)。
In the specific case asked in the question, $COLUMNS is a special variable which has nonstandard properties (see lhunath's answer above).
在问题中提到的特定情况下,$ COLUMNS是一个具有非标准属性的特殊变量(参见上面的lhunath答案)。
#3
Note that COLUMNS
is:
请注意,COLUMNS是:
- NOT an environment variable. It is an ordinary bash parameter that is set by bash itself.
- Set automatically upon receipt of a
SIGWINCH
signal.
不是环境变量。它是一个普通的bash参数,由bash本身设置。
收到SIGWINCH信号后自动设置。
That second point usually means that your COLUMNS
variable will only be set in your interactive shell, not in a bash script.
第二点通常意味着您的COLUMNS变量将仅在交互式shell中设置,而不是在bash脚本中设置。
If your script's stdin
is 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 ;-))
#4
The following script works for me for multiple values of $COLUMNS
. I wonder if you are not setting COLUMNS
prior 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 $COLUMNS
inside your script to see if it set correctly?
你可以在脚本中回显$ COLUMNS以查看它是否设置正确吗?
#5
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).
通过这种方式,您的代码更易于阅读并且更加模块化(自上而下的方法),您可以单独测试diff-columns代码(自下而上的方法)。