在bash脚本获取当前目录名(不完整路径) [英] Get current directory name (without full path) in Bash Script
本文介绍了在bash脚本获取当前目录名(不完整路径)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将如何在bash脚本得到的只是当前工作目录名称,甚至更好,只是一个终端的命令。
How would I get just the current working directory name in a bash script, or even better, just a terminal command.
PWD
给出当前工作目录的完整路径,例如的/ opt / local / bin目录
但我只想斌
pwd
gives the full path of the current working directory, e.g. /opt/local/bin
but I only want bin
推荐答案
无需基名,尤其是不需要一个子shell运行PWD(其中的增加了一个额外的,且价格昂贵,操作叉);外壳可以做到这一点内部使用参数扩展:
No need for basename, and especially no need for a subshell running pwd (which adds an extra, and expensive, fork operation); the shell can do this internally using parameter expansion:
result=${PWD##*/} # to assign to a variable
printf '%s\n' "${PWD##*/}" # to print to stdout
# ...more robust than echo for unusual names
# (consider a directory named -e or -n)
printf '%q\n' "${PWD##*/}" # to print to stdout, quoted for use as shell input
# ...useful to make hidden characters readable.
这篇关于在bash脚本获取当前目录名(不完整路径)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文