Bash: get all paths from path
问题 Say I have the path gui/site/junior/profile.py How do I get this?: gui gui/site gui/site/junior Bonus if you tell me how to loop through each path :D 回答1: You can loop with awk: awk 'BEGIN{FS=OFS="/"} { for (i=1; i<=NF; i++) { for (j=1; j<i; j++) printf "%s/", $j printf "%s\n", $i } }' <<< "gui/site/junior/profile.py" See as one liner: $ awk 'BEGIN{FS=OFS="/"}{for (i=1; i<=NF; i++) { for (j=1; j<i; j++) printf "%s%s", $j, OFS; printf "%s\n", $i}}' <<< "gui/site/junior/profile.py" gui gui/site