得到的文件路径文件目录路径
问题描述:
在bash编程如果 VAR =/家/我/ MYDIR / file.c中
,结果
如何获得/家/我/ MYDIR
。
In bash programming If VAR="/home/me/mydir/file.c"
,
How to get the "/home/me/mydir"
.
答
目录名
和基本名
是工具,你是在寻找用于提取路径组件:
dirname
and basename
are the tools you're looking for for extracting path components:
$ VAR=/home/me/mydir/file.c
$ DIR=$(dirname "${VAR}")
$ echo "${DIR}"
/home/me/mydir
$ basename "${VAR}"
file.c
他们不是的内部的庆典
命令,但应可在绝大多数系统将运行庆典
。
They're not internal bash
commands but should be available on the vast majority of systems that will be running bash
.