cd from within sub #29

cmalven opened this Issue Dec 8, 2012 · 6 comments


None yet

4 participants

cmalven commented Dec 8, 2012

This isn't so much an issue as a plea for assistance.

I'm working on a sub script that I can use for managing my various projects. One aspect of that involves navigating to the directory for a project.

I would use it like so:

cmal proj my-project

This command opens the project folder in Sublime Text, opens the dev url for the project in my browser, and ideally would cd into the directory for this project.

That last part is the tricky one, because the cd command is being run in a subshell, so once the cmal proj script finishes running I'm still in the same directory I started in.

Any tips on getting around this within the workings of sub?

In case it's not obvious, I'm a designer, not a command line guru. I know that normally I could get around this by using source my-script or . my-script but that's about all I know. Thanks.

mislav commented Dec 8, 2012

If you want to change the state of you current shell, like set an environment variable or change directory, you can't call a command to do that (as you've already discovered). It has to be handled by a shell function or eval.

Fortunately, sub supports this via "sh-*" commands. Write a command as you normally would that starts with "sh-", and that command should output shell commands that will get evaled in your shell by sub.

Take a look at "cd" command in mislav/coral

cmalven commented Dec 8, 2012

Fantastic! Thanks @mislav

Is there any way to prevent parts of my script from being evaled? For instance, my script drops into Ruby to do some side work and put things to the screen, and this seems to be screwing up my sh-proj command.

Alternatively, does sub let you call one of it's commands from within a sub command? For instance, could I call sh-cd from within proj?

mislav commented Dec 8, 2012

The script can do many things. Only its output to stdout will be
eval'ed. Make sure it only outputs the cd shell command after it does
all the work!

No the sh- command needs to be directly called. If you call it from
another command, the effect will be lost.

djtal commented Mar 21, 2013


Thanks for the explain but I'dont understand how to cd to a directory in a sub command

Edit now it can cd. But I've lost autocompletion.

Here what I've tried so far

My command is name tech-sh-open.

# Provide tech completions                                                                                                                                                                                                                                                  
if [ "$1" = "--complete" ]; then                                                                                                                                                                                                                                            
  find ~/dev/{plugins,projets,r-et-d} -mindepth 0 -maxdepth 1 ! -name '.*'  -exec basename {} \;                                                                                                            
fi   dir=$(find ~/dev -mindepth 1 -maxdepth 2 -name "$1" | head -n 1)

if [ -e "$dir" ]; then
  { [ -d "$dir/trunk" ] && app="$dir/trunk"; } || { [ -d "$dir/webapp" ] && app="$dir/webapp"; }
  echo cd ${app}"
  echo "Aucun projet '$1' trouvé..." >&2
  exit 1

Any hints on the possible error ?

djtal commented Mar 21, 2013

searching through github lead me to

cybera/sub@bd1c3eb which seem to allow autcompleting sh-* commands.
It work on my command

I was able to do this by

cd $dir

of course you have to type exit to end the execution of the script

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment