bash: extracting last two dirs for a pathname

前端 未结 7 953
灰色年华
灰色年华 2021-02-12 12:42

I seem to have failed at something pretty simple, in bash. I have a string variable that holds the full path to a directory. I\'d like to assign the last two di

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-12 13:11

    dirname=/a/b/c/d/e
    IFS=/ read -a dirs <<< "$dirname"
    printf "%s/%s\n" "${dirs[-2]}" "${dirs[-1]}"
    

提交回复
热议问题