Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I work in a windows environment but prefer to use bash in cygwin as my shell. To cd to a lengthy path I am given in windows form I type:

cd $(cygpath -u 'Z:\Some\long\windows\path')

..pasting the windows path in from the clipboard. Since I'm lazy I tried to make a shortcut thus:

#!/usr/bin/env bash
if [ -z "$1" ]; then
    exit 0
else 
    TGT=$(cygpath -u $1)    
    cd $TGT
fi
exit 0

..then realised that the cd takes place in the shell my script runs in rather than the one I called it from so does not have the desired effect. How do I make this work?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Plain cd 'Z:\Some\long\windows\path' should just work actually. You might want to enable the nodosfilewarning option though.

share|improve this answer
    
Good heavens.. so it does. I've asked a stupid question then. –  StamfordBingo Dec 16 '10 at 16:23
add comment

You could just set it as an alias:

alias cd='cd $(cygpath -u "$1")'

Stick that in your .bashrc file or wherever else you have your aliases set, and you should be golden.

Otherwise you'd need to put the directory with the path to your custom script earlier in your path than the directory with the actual cd command (I think cygwin sets path in .bash_profile); which probably won't work since cd is a shell builtin.

share|improve this answer
    
Doesn't work for me. What am I missing? I added.. alias cwd='cd $(cygpath -u "$1")' ..to .bash_profile where my other aliases are. $ cwd 'c:\temp\' cygpath: can't convert empty path –  StamfordBingo Dec 16 '10 at 16:01
    
Did you logout and back in again or run source ~/.bash_profile? –  Jed Daniels Dec 16 '10 at 16:04
    
If you type 'alias', do you see cwd listed? –  Jed Daniels Dec 16 '10 at 16:04
    
I ran source ~/.bash_profile. I've also now closed and restarted my session. ..Yes, if I type alias I see alias cwd='cd $(cygpath -u "$1")' –  StamfordBingo Dec 16 '10 at 16:12
    
Aliases don't accept positional parameters. This could be made into a function, however. –  Dennis Williamson Dec 16 '10 at 18:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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