这个问题已经有了答案:
Difference between single and double quotes in Bash
6个答案
我有一个脚本,它显示目录中文件名包含5个或更少字符的每个文件:
#!/bin/bash
ls | egrep '^.{0,5}$'
示例目录:
foobar
foo
bar
输出:
foo
bar
但当我试图用一个变量代替5时,如下图所示,我没有得到输出。没有错误,所以我假设它还在寻找某种范围。但不是0-5。
#!/bin/bash
declare -i num=5
ls | egrep '^.{0,$num}$'
如何在花括号内使用变量?
最佳答案
大括号不是这里的问题,而是单引号。使用双引号-''
中的变量不由shell展开:
ls | egrep "^.{0,$num}$"
另见:
Difference between single and double quotes in Bash
关于linux - 在Bash脚本中的花括号内使用int变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42615644/