如何从Bash中的路径字符串中删除文件后缀和路径部分? [英] How do I remove the file suffix and path portion from a path string in Bash?

查看:212
本文介绍了如何从Bash中的路径字符串中删除文件后缀和路径部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个字符串文件路径,例如/foo/fizzbuzz.bar,我如何使用bash来提取所述字符串的fizzbuzz部分?

解决方案

以下是如何在Bash中使用#和%运算符。

  $ x =/ foo / fizzbuzz.bar
$ y = $ {x%.bar}
$ echo $ {y ## * /}
fizzbuzz

$ {x%.bar} 也可以 $ {x%。*} 可删除点或 $ {x %%。*} 后的所有内容。



示例:

  $ x =/ foo / fizzbuzz .bar.quux
$ y = $ {x%。*}
$ echo $ y
/foo/fizzbuzz.bar
$ y = $ {x %%。 *}
$ echo $ y
/ foo / fizzbuzz

位于 Bash手册。查找 $ {parameter%word} $ {parameter %% word} 尾部匹配部分。 >

Given a string file path such as "/foo/fizzbuzz.bar", how would I use bash to extract just the "fizzbuzz" portion of said string?

解决方案

Here's how to do it with the # and % operators in Bash.

$ x="/foo/fizzbuzz.bar"
$ y=${x%.bar}
$ echo ${y##*/}
fizzbuzz

${x%.bar} could also be ${x%.*} to remove everything after a dot or ${x%%.*} to remove everything after the first dot.

Example:

$ x="/foo/fizzbuzz.bar.quux"
$ y=${x%.*}
$ echo $y
/foo/fizzbuzz.bar
$ y=${x%%.*}
$ echo $y
/foo/fizzbuzz

Documentation can be found in the Bash manual. Look for ${parameter%word} and ${parameter%%word} trailing portion matching section.

这篇关于如何从Bash中的路径字符串中删除文件后缀和路径部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆