Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing calculated paths with spaces

I'm trying something really simple here, but can't figure out where I'm going wrong. I've found many other useful discussions of this - particularly here - but haven't found anything that covers my specific scenario.

In powershell, I have typed the following:

$path = "c:\program files\"
$path2 = "c:\program files\fred2\"
echoargs $path $path2
echoargs "$path" "$path2"

In both calls to echoargs, I get

Arg 0 is <c:\program files" c:\program>
Arg 1 is <files\fred2">

back as the result. How can I get the parameters to be passed through correctly?

NB: In my real script the path variables are built up from a few config parameters, so I can't just pass them directly in single quotes.

like image 752
James Crowley Avatar asked Jan 16 '12 16:01

James Crowley


People also ask

How to deal with spaces in file path?

Use quotation marks when specifying long filenames or paths with spaces. For example, typing the copy c:\my file name d:\my new file name command at the command prompt results in the following error message: The system cannot find the file specified.

How do you pass a space with a path in powershell?

In case you want to run powershell.exe -File from the command line, you always have to set paths with spaces in double quotes ("").

How would you display the value stored in a variable in Powershell console?

To display the value of a variable, type the variable name, preceded by a dollar sign ( $ ). To change the value of a variable, assign a new value to the variable. The following examples display the value of the $MyVariable variable, changes the value of the variable, and then displays the new value.


1 Answers

You need to enclose your result strings in single quotes inside the scope of the execution:

echoargs "'$path'" "'$path2'"

This will pass them to the called application delimited inside single quotes, but since the entire string is still in double quotes your parameter will be expanded correctly.

like image 200
JNK Avatar answered Sep 18 '22 04:09

JNK