Bash:如何在算术表达式中进行变量扩展?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/673016/
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
Bash: How to do a variable expansion within an arithmetic expression?
提问by Jukka Dahlbom
While attempting to write a simple bash script to help with my deployment process, I ran in to a confusing error:
在尝试编写一个简单的 bash 脚本来帮助我的部署过程时,我遇到了一个令人困惑的错误:
#!/bin/bash
WEEKDAY=$(date +'%u')
echo $WEEKDAY
DAYS_TO_WEDNESDAY=$((3-$WEEKDAY))
echo $DAYS_TO_WEDNESDAY
Results in:
结果是:
1
")syntax error: invalid arithmetic operator (error token is "
The strangest part of it is that I could swear that this very script ran perfectly well some days ago.
最奇怪的是,我可以发誓几天前这个脚本运行得非常好。
EDIT (14:58):
编辑(14:58):
The issue was not with the bash script, but with using SVN through TortoiseSVN. The detour through Windows changed EOL markers to "CR LF", which result in syntax errors in bash. Setting svn:eol-style -property helps avoid further similar issues.
问题不在于 bash 脚本,而在于通过 TortoiseSVN 使用 SVN。绕过 Windows 将 EOL 标记更改为“CR LF”,这导致 bash 中的语法错误。设置 svn:eol-style -property 有助于避免进一步的类似问题。
回答by lhunath
Your error message seems to indicate pollution of your data with CRs.
您的错误消息似乎表明您的数据受到 CR 的污染。
")syntax error: invalid arithmetic operator (error token is "
Notice how the stuff that's supposed to come after the current end of your line is at the beginning. That's most likely because your error token is in fact a CR (which is a carriage return character - a character that instructs the terminal to put the cursor on the beginning of the line). These characters are almost only used by Windows machines where they are part of line endings.
请注意在当前行尾之后应该出现的内容是如何出现在开头的。这很可能是因为您的错误标记实际上是一个 CR(这是一个回车符 - 一个指示终端将光标放在行首的字符)。这些字符几乎只被 Windows 机器使用,它们是行尾的一部分。
I will assume that you're working on a windows machine and that your "date" command gave the output followed by a "windows" newline, which is actually a \r\n (carriage return, newline). The $() always strips trailing newlines, which leaves the \r at the end causing parsing problems in your script.
我假设您在 Windows 机器上工作,并且您的“date”命令给出了输出,后跟“windows”换行符,这实际上是一个 \r\n (回车,换行符)。$() 总是去掉尾随的换行符,这会在末尾留下 \r,从而导致脚本中出现解析问题。
Here, the following command produces your error on UNIX:
在这里,以下命令会在 UNIX 上产生您的错误:
$ foo=$'5\r'; echo $((5+foo))
")syntax error: invalid arithmetic operator (error token is "
To resolve the issue, you need to get rid of the \r in your data. You can use parameter expansion for this, or tr(1).
要解决此问题,您需要删除数据中的 \r。您可以为此使用参数扩展或 tr(1)。
$ foo=$'5\r'; echo $((5+${foo//$'\r'}))
10
$ $ foo=$'5\r'; echo $((5+$(tr -d '\r' <<< "$foo")))
10
回答by slim
You can omit the $ within an arithmetic expression.
您可以省略算术表达式中的 $。
So:
所以:
DAYS_TO_WEDNESDAY=$((3 - WEEKDAY))
回答by JasonSmith
Copied and pasted your code and it works fine:
复制并粘贴您的代码,它工作正常:
$ cat > test.sh
#!/bin/bash
WEEKDAY=$(date +'%u')
echo $WEEKDAY
DAYS_TO_WEDNESDAY=$((3-$WEEKDAY))
echo $DAYS_TO_WEDNESDAY
^D
$ bash test.sh
1
2
Maybe $WEEKDAYhad bad data in it?
也许里面$WEEKDAY有坏数据?

