File paths with spaces in a variable | Unix Linux Forums | OS X (Apple)

  Go Back    


OS X (Apple) OS X is a line of Unix-based graphical operating systems developed, marketed, and sold by Apple.

File paths with spaces in a variable

OS X (Apple)


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-03-2012
tillett22 tillett22 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 3 December 2012, 7:55 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
File paths with spaces in a variable

Hi all my very first post so go easy on me!!

I am trying to build a very simple script to list a file path with spaces in. But I can't get around this problem. My script is as follows:

#!/bin/bash
X="/Library/Users/Application\ Support/"

LS="ls"
AL="-al"

$LS $AL $X

The response I get is this

ls: /Library/Application\: No such file or directory
ls: Support/: No such file or directory

I know this is a problem with the space in the file path but I thought the "" would pass to the commandline with no issues but I am wrong.
Sponsored Links
    #2  
Old 12-03-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 9:27 AM EDT
Location: Aachen, Germany
Posts: 4,406
Thanks: 73
Thanked 1,076 Times in 1,015 Posts
Try double quoting to preserve spaces:
Code:
$LS $AL "$X"

Sponsored Links
    #3  
Old 12-03-2012
elixir_sinari's Avatar
elixir_sinari elixir_sinari is offline Forum Advisor  
Registered User
 
Join Date: Mar 2012
Last Activity: 9 October 2014, 4:50 PM EDT
Location: India
Posts: 1,412
Thanks: 101
Thanked 496 Times in 473 Posts
And the back-slash is unnecessary in the path:

Code:
X="/Library/Users/Application Support/"

    #4  
Old 12-03-2012
tillett22 tillett22 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 3 December 2012, 7:55 AM EST
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
That has worked Thank you !!

So am I quoting in the wrong place? Should I quote when the variable is defined or when it is read

i.e. "$X" and $X
Sponsored Links
    #5  
Old 12-03-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 21 October 2014, 9:27 AM EDT
Location: Aachen, Germany
Posts: 4,406
Thanks: 73
Thanked 1,076 Times in 1,015 Posts
When defining, you can use quotes, but also escape spaces like "\ ". When referencing, double quotes keep the shell from splitting the variable's contents into several words at spaces.
Sponsored Links
    #6  
Old 12-04-2012
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 21 October 2014, 5:38 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 4,285
Thanks: 45
Thanked 824 Times in 651 Posts
Quote:
Originally Posted by tillett22 View Post
Hi all my very first post so go easy on me!!
No problem: would you please use CODE-tags when posting code? Thank you.

Quote:
Code:
#!/bin/bash
X="/Library/Users/Application\ Support/"

LS="ls"
AL="-al"

$LS $AL $X

I know this is a problem with the space in the file path but I thought the "" would pass to the commandline with no issues but I am wrong.
True. The problem is in the last line: your definition of "X" is ok, since the space is protected there (try removing the double quotes and you will get an error if you remove the escape "\" too). But when your script gets to the last line it first replaces "$X" with its contents and only then executes the line. This is "ls" is presented two parameters, separated by a space (which was no longer protected) the first and the second part of "$X". "ls" is fine with getting two paths, but these two paths, "/Library/Users/Application" and "Support/" do not exist, hence you get two errors, one for the first missing path and one for the second.

Use


Code:
$LS $AL "$X"

or, if you are completely paranoid, like me:


Code:
"$LS" "$AL" "$X"

and it will work as expected.

I hope this helps.

bakunin
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using sed to replace a string in file with a string in a variable that contains spaces vivelafete Shell Programming and Scripting 2 11-24-2009 08:43 AM
Help setting variable from file contents with spaces nrogers64 Shell Programming and Scripting 4 11-02-2009 02:35 PM
Removing blank spaces, tab spaces from file NARESH1302 Shell Programming and Scripting 3 08-07-2009 11:18 AM
Unable to use mimesender to send attachments with spaces in the file names / paths rsmorra UNIX for Dummies Questions & Answers 0 03-04-2009 03:47 PM
Strip leading and trailing spaces only in a shell variable with embedded spaces jerardfjay Shell Programming and Scripting 6 03-07-2005 01:24 PM



All times are GMT -4. The time now is 12:47 AM.