cygwin - Bash can't cd into a directory through another script -
this question has answer here:
- why doesn't “cd” work in bash shell script? 27 answers
a while ago wrote script (let's call myscript) cd specific folder. saved in cygwin home directory run calling ~/myscript. had problems it, got working calling
. ~/myscript
i have written parent script (let's call parentscript) few tasks , wanted add option cd specific directory calling myscript inside it.
myscript looks this:
cd /cygdrive/c/users/user_name
parentscript has this:
if [ "${1}" -eq cd_to_user_name_option ]; . ~/myscript fi this gives me same problem had before myscript. cds in subshell, exits leaving me in directory started with. want able run myscript without using parent that's why didn't put in parentscript (like grep -e , egrep). doing wrong??
you need invoke parentscript sourcing well:
. parentscript then, in whatever script contains that, need make sure first argument is
./grandparentscript cd_to_user_name_option a script invoked other means besides sourcing run in new process. process has own current working directory (man 3 getcwd). directory inherited parent process, parent doesn't child when child exits. way have inner script change working directory of outer script running them in same process. done sourcing, or . command, you've discovered.
another solution use shell function directory change:
magiccd() { cd my/special/place } however, avoid mixing procedural code data, maybe best choice use builtin cd command , store desired destination in variable.
my_special_place="$home/my/special/place" cd "$my_special_place" this last abstract sourced script, function or alias, , more obvious maintenance programmer comes along.
Comments
Post a Comment