Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to input a path with a white space?

Tags:

shell

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

like image 486
Federico Lenzi Avatar asked Oct 15 '12 18:10

Federico Lenzi


People also ask

Can Linux paths have spaces?

In the Linux operating system, we can run commands by passing multiple arguments. A space separates each argument. So, if we give the path that has a space, it will be considered two different arguments instead of one a single path.

How do you represent a space in bash?

To cd to a directory with spaces in the name, in Bash, you need to add a backslash ( \ ) before the space. In other words, you need to escape the space.


2 Answers

Use one of these threee variants:

SOME_PATH="/mnt/someProject/some path" SOME_PATH='/mnt/someProject/some path' SOME_PATH=/mnt/someProject/some\ path 
like image 119
Igor Chubin Avatar answered Oct 16 '22 00:10

Igor Chubin


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

like image 28
Tomek Wyderka Avatar answered Oct 15 '22 23:10

Tomek Wyderka