Been using unix cygwin for a time and added many tools recently, command line and scripting are much more familiar. Scripting and programming is my number one goal and 100k dollar question? I am not weened from windows yet though.

The post here -> solved a users dilemma! did a lot to make using / switching paths and directories easier. Better than opening notepad++ and doing it! Specifically this command

# cd "$(cygpath -u 'c:\Program Files\')" 

by ben. works on the fly in bash for me so as to allow building scripts efficiently.

Can someone for programming sake break it down piecemeal?

I noticed there's 2 commands and - quotes and the parenthesis are 'separating' the $ , which I assume is a variable place holder???

I always end up thinking it is shell built in doing it, the easy answer, but still need to find out. And the double quotes outside are for what, while inner singe quotes are for space and literal interpretations by shell.

Exactly what is the shell doing start- to finish? Running a debug could be option I believe, how do I do that to show outputs? Thanks for the response.

Last, I did not mention what I used to try to convert paths. Saw stuff for sed strings but never worked due to complexity, this is much simpler.

A common misconception is that the $ sign means variable. It actually signals bash to perform an expansion (There are cases where variables can be referenced without the $ sign, but I won't get too off topic).

When bash sees the $ sign in front of a variable, it will replace the $VAR with the value (of the variable) and then execute the entire line.

In your example, bash will be doing a command substitution/expansion. Between the parenthesis is a command, bash will execute that command first, take the output and replace the $(command) with it, then execute the cd command.

You can observe this behavior by looking at a trace. Execute your script using bash -x or put set -o xtrace below the header of your script. Example:

set -o xtrace

This will output the exact commands that bash is executing during the scripts which means after substitutions/expansions have taken place.

As for the quotes, they are not 100% necessary, but there can be situations in which the command in your example would not work properly. And that is if there are any spaces in the path returned by cygpath -u 'c:\Program Files\'. Bash command arguments are usually delimited by spaces, and without the quotes, bash will interpret those spaces as a delimiter and not a literal space (also the reason why there are single quotes around the path specified for the cygpath command).

For example, if the returned path was c:\user\Documents and Settings, with out quotes bash would read the command as cd c:\user\Documents and Settings, in which it will think that c:\user\Documents, and, and Settings are all arguments to be passed to the cd command. This is a problem because the cd command is only supposed to have one argument.

Double quotes around the $ substitution prevents space interpretation.

  • Thank you so much. My question was answered 100 percent. Reading man pages for bash , xtrace or set for complete details, that will be very valuable. Still work to do on it , as I am not using scripts , everything typed , and I don't know where to put the xtrace? Tried without $ and it loops (no return outputed). Thanks again. – Sircutz Sep 14 '16 at 19:20
  • @Sircutz Place it right under the #!/bin/bash, I've edited my answer to reflect this. This only applies if you have a shell script (.sh) that you are executing. – Chip Shadd Sep 15 '16 at 14:49
  • I assumed the command needed to be scripted. I can handle that - make file , chmods for executable permission and run it. Can't wait to see what it says. Thanks again. – Sircutz Sep 15 '16 at 17:48
  • Built it and ran it. The xtrace output is fantastic but can not get "my shell script" to do what I wanted- navigate. Here is the script and then the output from bash. – Sircutz Sep 15 '16 at 18:32
  • #! /bin/bash set -o xtrace cd "$(cygpath -u 'C:\Documents and Settings\Administrator\My Documents\Downloads')" \; – Sircutz Sep 15 '16 at 18:32

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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