For example, let’s say I have a directory called tmp and I am on the home directory
$pwd
/my/home/directory/
$ls
tmpdir
and I have a tmp.sh that cds into the “tmp” directory
#!/bin/bash
cd tmp
and I run the script using:
$sh tmp.sh
after running this script, I am still in my home directory.
1) I want to understand why this doesn’t work thoroughly(I just roughly know it has to do with children process that is independent of parent process(is this even right?))
and
2) how can I go about accomplishing this task(being end up in the directory that a script cd-ed in upon the completion of execution of the script)?
That’s the working directory of your shell. When you execute a script a new shell is created for your script. You are changing the present working directory for rest of your code and not the parent shell.
To accomplish this you can
&&you script. So if the script executes successfully then only you end up in the new directory asYou should also go through the unix.se post – Why cd is not a program? for better understanding.