How-To get root directory of given path in bash?

前端 未结 6 1647
你的背包
你的背包 2021-01-04 01:02

My script:

    #!/usr/bin/env bash
    PATH=/home/user/example/foo/bar
    mkdir -p /tmp/backup$PATH

And now I want to get first folder of

6条回答
  •  Happy的楠姐
    2021-01-04 01:19

    I've found a solution:

        #/usr/bin/env bash
        DIRECTORY="/home/user/example/foo/bar"
        BASE_DIRECTORY=$(echo "$DIRECTORY" | cut -d "/" -f2)
        echo "#$BASE_DIRECTORY#";
    

    This returns always the first directory. In this example it would return following:

        #home#
    

    Thanks to @condorwasabi for his idea with awk! :)

提交回复
热议问题