I have a main file which uses(from the main I do a source) a properties file with variables pointing to paths.

The properties file looks like this:

TMP_PATH=/$COMPANY/someProject/tmp
OUTPUT_PATH=/$COMPANY/someProject/output
SOME_PATH=/$COMPANY/someProject/some path

The problem is SOME_PATH, I must use a path with spaces (I can't change it).

I tried escaping the whitespace, with quotes, but no solution so far.

I edited the paths, the problem with single quotes is I'm using another variable $COMPANY in the path

share|improve this question
up vote 43 down vote accepted

Use one of these threee variants:

SOME_PATH="/mnt/someProject/some path"
SOME_PATH='/mnt/someProject/some path'
SOME_PATH=/mnt/someProject/some\ path
share|improve this answer
    
the last one did it for me. thanks – CW0007007 Aug 13 '14 at 8:18
    
@CW0007007: Actually, the all three are correct – Igor Chubin Aug 14 '14 at 6:59
    
Yes but I had the \ escaping the space already. Wasn't saying the others were wrong. – CW0007007 Aug 14 '14 at 7:01

I see Federico you've found solution by yourself. The problem was in two places. Assignations need proper quoting, in your case

SOME_PATH="/$COMPANY/someProject/some path"

is one of possible solutions.

But in shell those quotes are not stored in a memory, so when you want to use this variable, you need to quote it again, for example:

NEW_VAR="$SOME_PATH"

because if not, space will be expanded to command level, like this:

NEW_VAR=/YourCompany/someProject/some path

which is not what you want.

For more info you can check out my article about it http://www.cofoh.com/white-shell

share|improve this answer
3  
This should really be marked as the correct answer. The detail of quoting the variables themselves seems to be the main thing that trips people up (me included!) – pospi Feb 5 '14 at 3:35
    
The crucial bit of information which helped me was the fact that the quotes used during definition of the variable do not become part of the value of the variable, so the variable has to be quoted again when used. – bassim Jul 1 '15 at 16:49

You can escape the "space" char by putting a \ right before it.

share|improve this answer
4  
I did that, and did't work – Federico Oct 15 '12 at 19:09
    
Did you wrap that path in any quotations by any chance? – Florin Stingaciu Oct 15 '12 at 19:13
1  
Resolved. The problem was in one reference was the variable without the quotes – Federico Oct 15 '12 at 19:34
SOME_PATH=/mnt/someProject/some\ path

should work

share|improve this answer
    
Yea I tried escaping the character, but in that case gives me "/mnt/someProject/some" without the " path" – Federico Oct 15 '12 at 19:08
    
It might have to do something with your OS then. A standard bash console should work. What OS are you using? – meza Oct 15 '12 at 19:12
    
I'm using ubuntu – Federico Oct 15 '12 at 19:20
    
Me too, and it works for me. How are you trying to use it? – meza Oct 15 '12 at 19:25

If the file contains only parameter assignments, you can use the following loop in place of sourcing it:

# Instead of source file.txt
while IFS="=" read name value; do
    declare "$name=$value"
done < file.txt

This saves you having to quote anything in the file, and is also more secure, as you don't risk executing arbitrary code from file.txt.

share|improve this answer
    
This could have been a solution. The problem is there are more things than assignations. Thanks! – Federico Oct 15 '12 at 19:39

If the path in Ubuntu is "/home/ec2-user/Name of Directory", then do this:

1) Java's build.properties file:

build_path='/home/ec2-user/Name\\ of\\ Directory'

Where ~/ is equal to /home/ec2-user

2) Jenkinsfile:

build_path=buildprops['build_path']
echo "Build path= ${build_path}"
sh "cd ${build_path}"
share|improve this answer

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.