How to input a path with a white space?

Shell

Shell Problem Overview


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

Shell Solutions


Solution 1 - Shell

Use one of these threee variants:

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

Solution 2 - Shell

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</sub>

Solution 3 - Shell

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

Solution 4 - Shell

SOME_PATH=/mnt/someProject/some\ path

should work

Solution 5 - Shell

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.

Solution 6 - Shell

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

  1. Jenkinsfile:

    build_path=buildprops['build_path'] echo "Build path= ${build_path}" sh "cd ${build_path}"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFederico LenziView Question on Stackoverflow
Solution 1 - ShellIgor ChubinView Answer on Stackoverflow
Solution 2 - ShellTomek WyderkaView Answer on Stackoverflow
Solution 3 - ShellFlorin StingaciuView Answer on Stackoverflow
Solution 4 - ShellmezaView Answer on Stackoverflow
Solution 5 - ShellchepnerView Answer on Stackoverflow
Solution 6 - ShellGeneView Answer on Stackoverflow