在bash for循环中访问*

时间:2011-07-08 10:30:15

标签: bash

我有一个像这样的循环bash:

for i in /long/path/filename*; do
    echo ${i%filename*}/other/path/c01/{magic}; 
done

现在我想将{magic}替换为*

匹配的文本

2 个答案:

答案 0 :(得分:2)

也许是这样的:

for i in /long/path/filename*; do
    A=${i/\/long\/path\/filename/}
    echo ${i%filename*}/other/path/c01/${A}
done

答案 1 :(得分:0)

for i in /long/path/filename*; 
do echo ${i%filename*}/other/path/c01/${i#/long/path/filename}; 
done

来自man bash

   ${parameter#word}
   ${parameter##word}
          The word is expanded to produce a pattern just  as  in  pathname
          expansion.  If the pattern matches the beginning of the value of
          parameter, then the result of  the  expansion  is  the  expanded
          value of parameter with the shortest matching pattern (the ‘‘#’’
          case) or the longest matching pattern (the ‘‘##’’ case) deleted.
          If parameter is @ or *, the pattern removal operation is applied
          to each positional parameter in turn, and the expansion  is  the
          resultant  list.   If parameter is an array variable subscripted
          with @ or *, the pattern removal operation is  applied  to  each
          member  of the array in turn, and the expansion is the resultant
          list.