74

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

Braiam
  • 1
  • 11
  • 47
  • 78
Federico Lenzi
  • 1,632
  • 4
  • 18
  • 34

6 Answers6

92

Use one of these threee variants:

SOME_PATH="/mnt/someProject/some path"
SOME_PATH='/mnt/someProject/some path'
SOME_PATH=/mnt/someProject/some\ path
Igor Chubin
  • 61,765
  • 13
  • 122
  • 144
50

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

Tomek Wyderka
  • 1,425
  • 1
  • 15
  • 21
  • 10
    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 05 '14 at 03:35
  • 2
    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 01 '15 at 16:49
22

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

Florin Stingaciu
  • 8,085
  • 2
  • 24
  • 45
4
SOME_PATH=/mnt/someProject/some\ path

should work

meza
  • 8,247
  • 1
  • 14
  • 23
  • 1
    Yea I tried escaping the character, but in that case gives me "/mnt/someProject/some" without the " path" – Federico Lenzi 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
1

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.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

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}"
Gene
  • 10,819
  • 1
  • 66
  • 58