3

This question already has an answer here:

I frequently have to cd from $HOME to a particular long directory path. So I thought I'd put a cdscript in $HOME to make getting there a little quicker.

cdscript:

#!/bin/sh
directory="/some/big/long directory path/that/I/use/frequently"
cd "$directory"

Set permissions: chmod 700 cdscript

./cdscript doesn't do anything. What am I missing? (Yes, those spaces in the path exist, and I can copy and paste the exact individual lines in the shell with success, so the path exists too). Also, is it more Unixey to just make a symbolic link to the directory instead of the above script, and cd to the link instead?

marked as duplicate by steeldriver, John WH Smith, muru, glenn jackman, slm Dec 23 '14 at 13:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • It could be because of spaces. Add -x at the first line: #!/bin/sh -xand runt it again. It starts debug mode, and you can see what it's really running. – Albert Dec 23 '14 at 12:44
  • Try again without the double quotes. – user95841 Dec 23 '14 at 12:47
4

just doing

./cdscript

won't work. basically you forked a new shell, in which you cd, then the shell (and new working dir) exit.

You need to use

. ./cdscript

(there is a leading dot, and a space)

The first dot means : run ./cdscript as if I typed it. The second dot is needed if . is not in your PATH var.

4

You would be better of creating an alias for this within your shell. For example in .bashrc, you could put;

alias cdscript='cd /really/long/file/path/'
  • Ok, but why is an alias better than a symbolic link in this case? (i.e. is this the best solution, and if so why? - not trying to be obnoxious, just genuinely curious and trying to use the shell the best I can) – Escher Dec 23 '14 at 13:26
  • A symlink would add an entry into your directory ; the alias wouldn't. It keeps the directory clean of "practical symlinks", yet again, it could also be a good idea depending on your case. – John WH Smith Dec 23 '14 at 13:33
  • I agree it's cleaner than having specific scripts to change into a directory, there may be a need to have a script if you're doing something more complex but for a simple cd an alias would, in my mind, a cleaner-simpler solution. – Chris Davidson Dec 23 '14 at 14:01
  • I use symlinks, because then I can use them from all programs, not just the shell. However alias solves the question directly. – ctrl-alt-delor Dec 23 '14 at 15:43
1

The script changes your current working directory but then it is restored upon exit. Instead of typing

cdscript 

try typing

 . cdscript

to run your script for the desired result.

Not the answer you're looking for? Browse other questions tagged or ask your own question.