为什么模式*([^ \ /])在我的交互式shell中工作,而不是脚本?

时间:2014-11-04 01:24:03

标签: bash string-substitution

我正在编写一个脚本,使用bash参数替换将工作目录剥离到有限的长度。它适用于命令行,但相同的替换在脚本中没有任何作用。

代码:

#!/bin/bash

# Limit of working directory length
LIMIT=10

dir=${1/#$HOME/\~}

# If it's too long, normalize it by stripping ~ and adding ...
if [ ${#dir} -gt $LIMIT ]; then
  dir=${dir/#\~/"..."}
fi

echo $dir

# Strip levels until short enough or can't strip anymore.
while [[ ( ${#dir} -gt $LIMIT ) && ( "$dir" != "$last_dir" ) ]]; do
  last_dir="$dir"
  # Strip a level off.
  dir=${dir/#...\/*([^\/])/"..."} <- broken line
  echo $dir
done

echo $dir

如果我这样做

test=".../School/CS352/Project1"
text=${test/#...\/*([^\/])/"..."}

我得到.../CS352/Project1,这就是我想要的。但是同一个sub在我的脚本中什么也没做。

问题:如何使代码中的标记行与上面的示例相同?

1 个答案:

答案 0 :(得分:1)

执行命令

shopt -s extglob

...打开这种支持。

相关问题