changing current working dir with a script
I am trying to create few scripts that would change the working directory of the main shell/terminal. Not able to do so. I tried the following
#!/bin/bash cd /home/mtk/work/project1
#!/bin/bash cd /home/mtk/templates/resusable/sampleCodes
But I am aware that the above would change it only for itself i.e. the one running currently, which would be
code.sh in this case.
Please let me know, how would I be able to change the current working directory for the parent calling shell.
Example Run and expectation:
$ pwd /home/mtk $ p1.sh $ pwd /home/mtk ### --> Expected /home/mtk/work/project1
My use case is: I would like to do some processing and then place the prompt to a pre-defined directory.
I see why it would not work. And
alias seems to be the solution for such case. But I would like to expand, my question:
I need to create a script that would change the working dir of the parent shell dynamically. The script will be say
mycd.sh . It would have command line arguments as
mycd.sh -c/-p1/-p2/ or some other user defined arguments.
-c would switch the shell working dir to the respective dir. File
mycd.sh would look like
.. some processing # parameter parsing if [ $param1 -eq "c" ]; then # change working dir of parent here else ... other conditions
I guess, there must be some way to change the shell working directory.
Please let me know, if anything is not clear.