I've created a cool python program that helps people navigate to other directories in an interactive way. When they get there I'd like to have them hit Enter and exit the program leaving them in the selected dir. However, you always end up in the same dir you started in b/c only the child process that python's running in actually changes directories and the parent process' directory remains unchanged.
Asked
Active
Viewed 619 times
1 Answers
2
Instead of running your program directly, source a wrapper script. Your program, when it completes, signals to the wrapper script what directory to cd
to. The sourced wrapper script does the cd
.
Another file browsing utility, Midnight Commander (mc
), has addressed the same issue. Here is its wrapper script:
MC_USER=`id | sed 's/[^(]*(//;s/).*//'`
MC_PWD_FILE="${TMPDIR-/tmp}/mc-$MC_USER/mc.pwd.$$"
/usr/bin/mc -P "$MC_PWD_FILE" "$@"
if test -r "$MC_PWD_FILE"; then
MC_PWD="`cat "$MC_PWD_FILE"`"
if test -n "$MC_PWD" && test -d "$MC_PWD"; then
cd "$MC_PWD"
fi
unset MC_PWD
fi
rm -f "$MC_PWD_FILE"
unset MC_PWD_FILE
As you can see, this defines a temporary file and passes that file name to mc
with the -P
option. Before mc
is exits, it writes the chosen directory to that temporary file. This scripts reads that temporary file and cd's to the chosen directory.
To make it convenient to run this wrapper script, one creates a shell alias:
alias mc=". /usr/lib/mc/bin/mc-wrapper.sh"'
With slight modifications, I expect that you can make this work with your program.

John1024
- 109,961
- 14
- 137
- 171