Answers:
Bash无需调用外部即可获得路径的最后一部分basename:
subdir="/path/to/whatever/${1##*/}"
d=/home/me/somefolder;subdir="/$d/${1##*/}"我结束了类似//home/me/somefolder//的$ d实际上来自循环for d in $(find $SOMEFOLDER -maxdepth 1 -type d);使用subdir=$(basename $d)的作品如预期。
while而不是对(更好)for的输出进行迭代或使用globbing :(最后的斜杠的工作原理是-如果您可以在Bash 4中进行递归操作,但是会出现“参数列表过长”的消息可能)。请注意,命令的一部分代表脚本或函数的第一个参数。您可能需要使用或某些其他变量或参数规范,或者确保传入了参数findfind -print0 | xargs -0for d in $SOMEFOLDER/*/-type d**shopt -s globstar${1}${d##*/}$1
$1包含the path from which last component is to be taken out。我错过了那部分。我用例:target_path='/home/user/dir1/dir2/dir3/'; target_path="${target_path%/}"; last_component=${target_path##*/}; echo $last_component-作品😉
${1##*/}的作品:unix.stackexchange.com/a/171786/15070
SUBDIR="/path/to/whatever/$(basename $1)"