1

I'm thinking of writing a script for cygwin to cd into a windows directory which is copied from Windows explorer.

e.g.

cdw D:\working\test 

equals to

cd /cygdrive/d/working/test 

But it seems for shell script, all backslashs in parameters are ignored unless using single quote 'D:\working\test' or double backslashs D:\\working\\test.

But in my case it would be very inconvenience because I can't simply paste the directory name in the command line to execute the script.

Is there any way to make cdw D:\working\test working?

5
  • 2
    \ is an escape character in bash, so I think you're out of luck. Commented Aug 5, 2012 at 6:16
  • You could write a shell script taking in the path from stdin. However, you would have to run cdw without parameter and then paste the path as an input. Commented Aug 5, 2012 at 6:22
  • One warning, in the case that cdw is run as a separate script, it will run as a separate process. While cdw may actually chdir to the correct location, this will not affect the current directory of the process that called it. You'd need to define cdw as a function for that. Commented Aug 5, 2012 at 9:14
  • @HenkLangeveld Thanks for reminding. For me I will do it by alias cdw=. /home/deqing/scripts/cdw.sh, so it can still stay in a script. Commented Aug 5, 2012 at 9:56
  • Sorry @Deqing, but that won't work. But another question: How will you get the path to the script, just copy/paste? Commented Aug 5, 2012 at 10:02

3 Answers 3

6

Well, you can do it, but you want something strange :)

cdw() { set $(history | tail -1 ) shift 2 path="$*" cd $(cygpath "$path") } 

Example of usage:

$ cdw D:\working\test $ pwd /cygdrive/d/working/test 

The main point here is the usage of history. You don't use an argument directly, but get it from the history in the form it was typed.

$ rawarg() { set $(history | tail -1 ); shift 2; echo "$@"; } $ rawarg C:\a\b\c\d C:\a\b\c\d 

Of course, you can use this trick in a interactive shell only (for obvious reasons).

Sign up to request clarification or add additional context in comments.

9 Comments

it's not safe, if the file name contains a semicolon ';' for example
@NahuelFouilleul: No, it's safe (or if you are sure that it is not, please provide an example), but of course there are cases when it work incorrect. That's why I've written: "you want something strange"
@NahuelFouilleul: or better `C:\WINDOWS\; rm -rf /' :)
I don't get how can cd $(cygpath "$path") work. Shouldn't cd argument be double-quoted: cd "$(cygpath "$path")"?
@antonio: Ok, than try please: a=$(cygpath "$path"); cd "$a"
|
1

The problem you deal with is related to the shell. Any argument you add to cdw on the command line, will be processed by the shell before cdw gets executed.

In order to prevent that processing to happen, you need at least one level of quoting, either by enclosing the whole string in single quotes:

cd 'D:\working\test' 

or with double backslashses:

cd D:\\working\test 

A separate program will not help, because the damage is already done before it runs. ;-)

However, I have a possible function for cdw, which works in my AST UWIN ksh:

function cdw { typeset dir read -r dir?"Paste Directory Path: " cd ${dir:?} } 

And this one works in Bash (which does not support read var?prompt):

function cdw { typeset dir printf "Paste Directory Path: " read -r dir || return cd ${dir:?} } 

For me, I just type the two single quotes around the Pasted value.

1 Comment

You can solve the task without quoting, but, yes, it is a little bit strange; use quotes would be more correct of course
0

The solution to add single quotes allows to copy paste

3 Comments

You can do this without quoting; see my solution
yes, but I don't understand the need, and it's easier to type the single quotes
It was Deqing who wanted this, I just provided the solution

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.