Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 847  / 1 Year ago, mon, february 27, 2023, 1:40:28
pwd | grep 'database$'
if [ "$?" = 0 ]; then
echo "has db"
cd ..
cd ..
echo "after has db"
fi


after I execute it I see



has db
after has db


and I'm still in the same dir, how to fix it?


More From » bash

 Answers
3

I think it has to do with how you run the script.



If you do:



sh ./script.sh


Or



./script.sh


Then it invokes a new shell, runs the script, then returns you to the current one. So the cd is done in the new shell and your current one is untouched.



Try this instead:



. ./script.sh


Which will run script.sh in your current shell, and you'll keep the results after it finishes.


[#41968] Monday, February 27, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
torlim

Total Points: 408
Total Questions: 113
Total Answers: 110

Location: Estonia
Member since Wed, May 27, 2020
4 Years ago
;