1

I have a file in /usr/bin called code, which contains the following:

cd ~/Documents/Code

However, when I run it by simply executing

$ code

Nothing happens. Why is this, and how can I make code perform as I would like?

2
  • If that file is in /usr/bin because it's on the PATH and executable, you can run it in your current shell with . $(which code), but there are better ways to cd, one is mentioned by Jonathon. Commented May 24, 2014 at 19:48
  • @Vytenis I think you should add this as an alternative answer. Being able to run a script in the current shell is another valid way of doing this. Commented May 24, 2014 at 19:55

2 Answers 2

4

When you run code, a new process is spawned to execute that script. The script changes directories (for his process) and returns. Your original shell's process still has the same working directory. Child processes cannot manipulate the state of their parent.

A better way to accomplish this would be with an alias. Add this to your ~/.bashrc:

alias code='cd ~/Documents/Code'
Sign up to request clarification or add additional context in comments.

Comments

1

If that file is in /usr/bin because it's on the PATH and executable, you can run it in your current shell with . $(which code) or source $(which code), but there are better ways to cd, one is mentioned by Jonathon.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.