When you change directory it is changed. Which means that tee will write to a new file.
Possible solutions:
- instead of
tee -a logfile.log
usetee -a <absolute_path_to_current_directory>/logfile.log
move the whole
tee
outside the loop:while do stuff done | tee logfile.log # bonus - you don't need -a here
return to initial location after
cd
. Which should be actually done withpushd
andpopd
do not do
cd
in main shell. Do all processing in subshell:(cd $location; <some processing>)
I myself prefer the last one.
On the other hand (and that might be even better) - rewrite the processing to avoid cd
at all.