I'm running a Bash script that's buried in a directory on my system. I need to be able to get the first section of the directory path.
The path returned by $0 in my script is as below. The "path/to/script" section is constant; the "/variable/path/to/folder" is permanent.
/variable/path/to/folder/main-folder/path/to/my-script.sh
------------------------
I need the underlined section. I've tried cut -d/ -f-4 and cut -d/ -f4-, but cut indexes from the front of the path, and I need to index from the back. (I don't know how many levels will be between / and main-folder, but I do know how many will be between main-folder and my-script.sh. The path here is accurate: there are two folders in main-folder before you get to my-script.sh.)