Hi, I want a simple script to change directories. I have four attempted scripts:
go1.sh:
#!/bin/bash
cd Documents
go2.sh
#!/bin/bash
cd Documents/
go3.sh
#!/bin/bash
./cd Documents
go3.sh
#!/bin/bash
./cd Documents/
I am using openSUSE 10.3 with the gnome terminal. I have run chmod 777 on my scripts. I am logged in as root.
Here are the outputs in the four cases:
cuda:~ # cd Documents
cuda:~/Documents # cd …
Yes, the sub-directory exists.
cuda:~ # ./go1.sh
cuda:~ #
cuda:~ # ./go2.sh
cuda:~ #
cuda:~ # ./go3.sh
./go.sh: line 2: ./cd: No such file or directory
cuda:~ #
cuda:~ # ./go4.sh
./go.sh: line 2: ./cd: No such file or directory
cuda:~ #
I am not a linux expert but have used it occasionally in the past. This is driving me nuts. I know it must be something simple or stupid (or both). Please help!
It won’t work that way. The current directory is inherited from the parent process but a child process cannot change the current directory of its parent. Since your cd is inside a script, a child process, nothing is changed in the parent on return to the parent.
Instead you have to use an alias to do this, which is executed in the current process.
Exactly. To see how this works outside of a script run the following:
bash #starts new shell
cd Documents #changes directory
exit #simulates the ending of your script
Notice that you are back where you started no mater where you were in
your second ‘bash’. You can see which ‘level’ of shell you are in with:
echo $SHLVL
which increments with each invocation of a new environment (each call to
‘bash’ in this case).
Good luck.
ken yap wrote:
> It won’t work that way. The current directory is inherited from the
> parent process but a child process cannot change the current directory
> of its parent. Since your cd is inside a script, a child process,
> nothing is changed in the parent on return to the parent.
>
> Instead you have to use an alias to do this, which is executed in the
> current process.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
That’s simply not how Unix is designed. Having a global current directory doesn’t go with a multiuser system. Even when restricted to a single user, it’s a security nightmare; imagine any shell script being able to change the current directory of some other process of the user. Linux is not Windows, it’s also not DOS.
Here’s how you would do it with an alias:
alias go='cd there'
then type:
go
to run the alias. In practice you would put the alias inside a bash init file so that it would be available for use for each session.
That DOS doesn’t implement shells properly is beyond debate… even
microsoft abandoned it.
Also, it’s fairly trivial. First, the obligatory requirement to read
the ‘man’ pages… and then the solution:
alias go=‘cd /path/to/your/freakin/long/directory’
To make it persistent add the line above to your ~/.bashrc file (’~’
means your home directory, so something like /home/youruser/.bashrc
would be the result).
Good luck.
skippy1729 wrote:
> ab@novell.com;1898650 Wrote:
> Exactly. To see how this works outside of a script run the following:
>
> bash #starts new shell
> cd Documents #changes directory
> exit #simulates the ending of your script
>
> Notice that you are back where you started no mater where you were in
> your second ‘bash’. You can see which ‘level’ of shell you are in
> with:
>
> echo $SHLVL
>
> which increments with each invocation of a new environment (each call
> to
> ‘bash’ in this case).
>
> Good luck.
>
>
>
>
>
> ken yap wrote:
>>>> It won’t work that way. The current directory is inherited from the
>>>> parent process but a child process cannot change the current
> directory
>>>> of its parent. Since your cd is inside a script, a child process,
>>>> nothing is changed in the parent on return to the parent.
>>>>
>>>> Instead you have to use an alias to do this, which is executed in
> the
>>>> current process.
>>>>
>>>>
> So, I can’t do this with linux? Even DOS 1.0 could do this!
> My actual path is six directories deep and well over 100 keystrokes. I
> can’t get there from here?
> Skippy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org