I know what I can make a shell procedure or source the script instead of running it to cd
the shell I am using. What I want to know is there any way to cd
the "parent shell" (not sure if that is the correct phrase).
#!/bin/sh
# This is script.sh
cd $1
pwd
Here's some output. (Note: this isn't the exact output, I just shortened it by stripping out unnecessary details. Lines that are my input into the shell begin with >
.)
> mkdir foo
> ./script.sh foo
/home/myName/foo
> pwd
/home/myName
This is expected behavior as I understand. Here is if I "source" the script.
> . ./script.sh foo
/home/myName/foo
> pwd
/home/myName/foo
Without sourcing the script and without making this a shell procedure, is there any way to type ./script.sh foo
result in the shell I am using be in the directory /home/myName/foo
?
>
$PS2
prompt there? In any case, no. You cannot change the parent's current working directory from the child context, and the parent calls a child shell to read the script. When you source the script instead the parent reads it rather than calling the child and so changes its own directory. If you want the parent to CD in response to some message or signal from the child you'll have to construct the format yourself. – mikeserv Jan 20 '16 at 17:11kill
signal or whatever. So you'll need some to construct some means of passing information to and from and the protocol is all yours to develop. Its doable, but its not done. – mikeserv Jan 20 '16 at 18:13