10楼正解
:lol: :lol: :lol: pwd | sed 's/\/[^\/]*$//' 原帖由 leyyer 于 2007-8-28 18:12 发表 http://linux.chinaunix.net/bbs/images/common/back.gifbash ?
a=/home/me/a.out
echo ${a%/*}
强哦……找到了相关解释(man bash),顶一下:
${parameter#word}
${parameter##word}
Thewordisexpanded to produce a pattern just as in pathname
expansion.If the pattern matches the beginning of the value of
parameter,thentheresultofthe 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
toeachpositional parameter in turn, and the expansion is the
resultant list.If parameter is an arrayvariablesubscripted
with@or*, the pattern removal operation is applied to each
member of the array in turn, and the expansion is theresultant
list.
${parameter%word}
${parameter%%word}
Thewordisexpanded to produce a pattern just as in pathname
expansion.If the pattern matches atrailingportionofthe
expanded value of parameter, then the result of the expansion is
the expanded value of parameter with the shortest matchingpat-
tern(the``%''case)orthelongest matching pattern (the
``%%'' case) deleted.If parameteris@or*,thepattern
removaloperationisappliedto each positional parameter in
turn, and the expansion is the resultant list.If parameteris
anarrayvariable subscripted with @ or *, the pattern removal
operation is applied to each member of the arrayinturn,and
the expansion is the resultant list.
${parameter/pattern/string}
${parameter//pattern/string}
The pattern is expanded to produce a pattern just as in pathname
expansion.Parameter is expanded and the longest match ofpat-
ternagainstitsvalue is replaced with string.In the first
form, only the first match is replaced.The second formcauses
allmatchesof pattern to be replaced with string.If pattern
begins with #, it must match at the beginningoftheexpanded
valueof parameter.If pattern begins with %, it must match at
the end of the expanded value of parameter.If string isnull,
matchesofpattern are deleted and the / following pattern may
be omitted.If parameter is @ or *, the substitutionoperation
isapplied to each positional parameter in turn, and the expan-
sion is the resultant list.If parameter is anarrayvariable
subscriptedwith@ or *, the substitution operation is applied
to each member of the array in turn, and theexpansionisthe
resultant list. :emn54: :emn58: :emn24: :emn25: :emn26: :em35: :em07: :em36: :em37: :emn54: 很简单的问题嘛,而且楼上的几个兄弟已经给了几个方法了 path="$(pwd -P)"
path="$(echo "${path%/*}")"
#弱引用是为了应付含有空格的目录的情况
[ 本帖最后由 jarhead1 于 2007-8-29 12:52 编辑 ] 没有dirname的情况下,2楼的答案是最佳答案。不同意的请举手。 path1=/a/b/c
path2=`echo $path1 | awk '{split($0,array,"/");print "/"array"/"array}'`
echo $path2
[ 本帖最后由 fogsky 于 2007-8-29 14:38 编辑 ]