Here's what I'm trying to do:I have a folder for code repositories, and I'm looking for a bash command that can tell the code root directory of the current working directory. For example, if I'm currently in ~/repositories/my_program/folder1/folder2/, I want the command to return ~/repositories/my_program/.I can get the working directory path with pwd and a list of projects using ls ~/repositories/. How do I determine which one of the ~/repositories/<match> strings is a prefix of pwd?
Bash command to determine which string in a group of strings is a prefix of another string
bash
If you just want the directory right under ~/repositories, you could do something like$ sed -r s#($HOME/repositories/[^/]*).*#\1# <<<$PWD/home/terdon/repositories/my_programAnd to print my_program only:$ sed -r s#$HOME/repositories/([^/]*).*#\1# <<<$PWDmy_programTo use ~/ instead, try$ sed -r s#$HOME(/repositories/[^/]*).*#~\1# <<<$PWD~/repositories/my_programThe idea is to match $HOME/repositories/, then one more directory (defined as the longest string of non-/ characters: [^/]*).