简体   繁体   中英

bash cd up to specific directory

I'm working on an install script (written in bash) for a library. The install script lives in a subdirectory of the project's base directory. I'd like users to be able to run the install script from anywhere under the base directory, so I'm trying to make the script cd to the base directory before executing other commands.

I'm looking at something like

while [[ $PWD == *"ProjectName"* ] && [ $PWD -ne *"ProjectName" ]]; do cd .. ;done

I'm open to cleaner solutions, but at the very least I'd like to understand why the above does not work. No doubt I have brackets in the wrong spots, but I'm not sure where to put them.

The following does what I want. Thanks to everyone (especially @David C. Rankin) for their helpful comments.

while [[ "$PWD" = *"ProjectName"* && ! "$PWD" = *"ProjectName" ]]
      do
      cd ..
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM