Federico Lenzi
Federico Lenzi

Reputation: 1612

How to input a path with a white space?

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

Upvotes: 73

Views: 198850

Answers (7)

RGD2
RGD2

Reputation: 469

There is no good answer for this, there are circumstances where every combination of \ , ' ' or even "$VAR" which seems to fix the issue, breaks later.

So, Avoid spaces in the path entirely:

On any unix system, you can make soft-links with ln -s

So do this for your badly-behaved path and then put the name of the new link (you have created somewhere with a spaceless path) into your variable instead.

This solves the case where you can't change the path.

This way the escape characters or quoting only need work for the one call to ln -s, probably in the same batch file you're going to export it.

Thereafter the variable will work, without renaming the path to the things it needs to include.

Upvotes: 0

Gene
Gene

Reputation: 11285

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}"

Upvotes: 0

Tomek Wyderka
Tomek Wyderka

Reputation: 1465

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

Upvotes: 51

chepner
chepner

Reputation: 531808

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.

Upvotes: 1

Igor Chubin
Igor Chubin

Reputation: 64603

Use one of these threee variants:

SOME_PATH="/mnt/someProject/some path"
SOME_PATH='/mnt/someProject/some path'
SOME_PATH=/mnt/someProject/some\ path

Upvotes: 95

Florin Stingaciu
Florin Stingaciu

Reputation: 8295

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

Upvotes: 22

meza
meza

Reputation: 8437

SOME_PATH=/mnt/someProject/some\ path

should work

Upvotes: 5

Related Questions