关于用bash中的文本去掉斜杠

时间:2014-01-21 07:15:30

标签: bash

我对linux不好,如果问题非常基本,我很抱歉。

我需要一个关于bash shell的命令,它将从/ home / barco / ext / linux-crp / bin / linux-x86-32 /.

返回/ home / barco / ext / linux-crp

我读取参数扩展但无法获得任何提示。

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

不完全清楚您希望它基于什么,但

> string="/home/barco/ext/linux-crp/bin/linux-x86-32/"; echo "${string%/*/*/*}"
> /home/barco/ext/linux-crp

将路径返回两级(假设路径以/结束且足够长)

${parameter%word}
${parameter%%word}
  Remove matching suffix pattern.  The word is expanded to produce
  a pattern just as in pathname expansion.  If the pattern matches
  a  trailing portion of the expanded 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 posi‐
  tional 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.