0

I have a file called go in ~/

All I want to do is to be able to run go and have it cd to this other dir.

This is what the go file looks like:

$ cat go
#!/bin/bash
cd ~/Desktop/rs3

I ran the line $ chmod +x go

And then the line ./go to attempt to run the file.

If i put echo whatever in the file it will print whatever to the console, but the cd command never works.

Thanks.

sherlock
  • 744
  • 7
  • 21
  • @StuartGolodetz cool, that pretty much answers the question, thanks! too bad none of the tutorials ive been looking at for 2 hours told me that. – sherlock Dec 13 '13 at 17:27
  • 1
    @sherlock: You can still run `. ~/go` to change directory – anubhava Dec 13 '13 at 17:29
  • The `cd` command in the script does work. It just applies only to the process running the script, not to its parent process (your interactive shell). You can use `.` to run the script in the context of your current shell (in which case the script probably *shouldn't* be executable and shouldn't have the `#!/bin/bash` line), *or* you can modify the script to print a `cd` command to standard output and then execute that command: `eval $(~/go)` (which you can wrap in a shell function for convenience). – Keith Thompson Dec 13 '13 at 21:14
  • In the latter case (`eval $(~/go)`), if it prints multiple commands they should be separated with semicolons, since `$(cmd)` joins the output of `cmd` into a single line. – Keith Thompson Dec 13 '13 at 21:15

1 Answers1

1

It doesn't work because the script runs in a subshell, so the environment is different.

What you can do is to alias go='cd ~/Desktop/3s3' - as it's an alias, the shell performs the substitution and runs the cd on itself, as if you've just typed it.

You should define the alias in your ~/.bashrc, ~/.bash_profile or any file that gets sourced when you login.

mgarciaisaia
  • 14,521
  • 8
  • 57
  • 81