ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199
Rep:
export filePath="/folder/folder with space/"
Exactly what the subject implies.
Ive tried escape characters, using quotes, using both...and everything I can see under the sun.
Literally spent 2 hours trying to figure this shit out...
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199
Original Poster
Rep:
nope
it says
cannot access /folder/folder\: No such file or directory
cannot access with\: No such file or directory
cannot access spaces/: No such file or directory
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199
Original Poster
Rep:
export thisVar = "/folder/folder\ with\ spaces/"
ls $thisVar
"/folder/folder\" does not exist
.....or
export thisVar = /folder/folder\ with\ spaces/
ls $thisVar
"/folder/folder" does not exist
.....or
export thisVar = "/folder/folder with spaces/"
ls $thisVar
"/folder/folder" does not exist
.......
none of the above methods recognize there is a space in the path
however:
ls /folder/folder\ with\ spaces/
works fine
ls "$thisvar" would be recognized properly. Without the quotes around the variable, ls will interpret the expansion of the variable like any other space-separated list of words. i.e. the difference between
ls filename with spaces
and
ls "filename with spaces"
There are two separate issues at play here. The first is the one about which vendtagain asked, namely how to assign a text value containing space characters to an environment variable. This seems to have been adequately answered.
The second issue is how the application deals with those strings containing embedded whitespace. This would depend entirely on how the string is used, especially if it is being parsed in some way. Unless vendtagain has some control over this, it is likely that he has done all that can be done, with the exception of using filesystem names/paths that do no contain embedded whitespace.
It may be feasible to attach enclosing quotes of some sort, which the end application may understand to mean 'use everything between the quotes'. Likewise, it may be possible to embed escapes of some sort, which the end application interpets. Backslash characters are the conventional escape characters used in most situations.
--- rod.
You can echo almost any string, certainly one with spaces, but ls and most *nix file handling tools eg mv, cp use whitespace as an arg separator ... unless you mess with IFS, but you'd need to know to do that every time.
That's why its very rare to see filenames with spaces in pure *nix systems.
They (the files) tend to have been imported from MS or created by an MS user.
That's why its very rare to see filenames with spaces in pure *nix systems.
They (the files) tend to have been imported from MS or created by an MS user.
My system is rare (or maybe I'm a covert MS user at heart )!
Code:
c:~$ find . -name '* *' 2>/dev/null | wc -l
14476
Serimously, quotes allow whitespace in pathnames in (exported) variables
Code:
c:~$ export FOO='d/LQ reply snippets'
c:~$ ls -l "$FOO"
-rw-r--r-- 1 c 1000 993 Nov 27 11:56 d/LQ reply snippets
Yep, you can have spaces in filenames if you want, but as I said, you'd have to ensure every single program allows for that, and not just bash tools either.
By default, *nix tools take a space as the arg separator; it's not enforced but it is the convention.
As for *nix files usually not having spaces, I'm talking about those from the suppliers/repos. It's very rare, especially on non-Linux eg Solaris, HP-UX etc.
Actually, isn't it the shell that break arguments on whitespace? If I launch a program from a shell, it builds the argc/argv parameters from whatever is specified on the commandline, and passes those to the launched program. The same applies if any other program launches with exec() (in one of its many flavors), another program. The parent builds up the argc/argv parameters, of which any number of argvs may contain whitespace, and those arguments will remain intact when the child process accesses them. I do not know of a single common application that then splits individual arguments on whitespace in the character arrays. Can you name any?
Proper use of quotes and escapes in the shell is a key concept to be learned when shell scripting.
--- rod.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.