|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| OS X (Apple) OS X is a line of Unix-based graphical operating systems developed, marketed, and sold by Apple. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Try double quoting to preserve spaces: Code:
$LS $AL "$X" |
| Sponsored Links | ||
|
|
#3
|
||||
|
||||
|
And the back-slash is unnecessary in the path: Code:
X="/Library/Users/Application Support/" |
|
#4
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
No problem: would you please use CODE-tags when posting code? Thank you. Quote:
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|