Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!xzaphod!michael From: michael@xzaphod.uucp (Michael R. Miller) Newsgroups: comp.unix.questions Subject: Re: using cd command in a file Summary: script won't work Message-ID: <1991May27.170005.12870@xzaphod.uucp> Date: 27 May 91 17:00:05 GMT References: <1991May20.201923.27920@garfield.ncat.edu> <9105230900.22@rmkhome.UUCP> Reply-To: michael@xzaphod.UUCP (Michael R. Miller) Organization: XZaphod in Rockville, MD Lines: 47 In article <9105230900.22@rmkhome.UUCP> rmk@rmkhome.UUCP (Rick Kelly) writes: >In article <1991May20.201923.27920@garfield.ncat.edu> muquit@garfield.ncat.edu (MUHAMMAD A. MUQUIT) writes: >>In article <1991May20.155136.25162@ux1.cso.uiuc.edu> Charles Blair writes: >>> >>> I would like to get to a directory /me/A/B/C/D by just typing j. I tried >>>creating a file j with cd /me/etc in it, then chmod +x j. It didn't work. >>>Thanks in advance. I'm sure I'm overlooking something well-known. >> >>You can do the job if you put this line in your .login file: >> alias j 'cd /me/A/B/C/D' >> >>I'm also curious why your way didn't work. I think there're lots of gurus >>out there to answer this. > >A two line script would do it. > >#!/bin/sh >cd /me/A/B/C/D > >Then chmod +x scriptname. > >Rick Kelly rmk@rmkhome.UUCP frog!rmkhome!rmk rmk@frog.UUCP A script won't work because "cd" is a "built in" command in every shell I've heard of. What happens when you run the script is your shell forks a copy of itself which then runs the commands in the script. The CHILD shell does the "cd" command. Then the CHILD shell exits. The shell that "knew" about the directory change has disappeared along with the directory change itself. The parent process (shell) still has its working directory set as it was prior to your script running. Nothing constructive has happened except another process has run and some files were opened and closed causing updates to buffers to possibly occur inside the kernel. A modification to the script command could be: #!/bin/sh exec $SHELL -c "cd /me/A/B/C/D" assuming the SHELL environment variable is set to the path to the default shell you want to use (eg. /bin/sh). This is not the most efficient way of doing this. If your shell supports aliases (/bin/ksh, /bin/csh, /bin/zsh), use it. Michael R. Miller uunet!xzaphod!michael