小编qit*_*pot的帖子

在Bash脚本中使用大括号内的int变量

我有一个脚本,显示目录中包含5个或更少字符的文件名:

#!/bin/bash

ls | egrep '^.{0,5}$'
Run Code Online (Sandbox Code Playgroud)

示例目录:

foobar
foo
bar
Run Code Online (Sandbox Code Playgroud)

输出:

foo
bar
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试使用变量代替5时,如下所示,我得不到输出.没有错误,所以我假设它仍在寻找某种范围.但不是0-5.

#!/bin/bash

declare -i num=5
ls | egrep '^.{0,$num}$'
Run Code Online (Sandbox Code Playgroud)

如何在花括号内使用变量?

linux bash

1
推荐指数
1
解决办法
308
查看次数

标签 统计

bash ×1

linux ×1