?
Solved

Environment variable question

Posted on 2002-07-17
11
Medium Priority
?
200 Views
Last Modified: 2013-12-15
I want to be able to use an environment variable in this way

cd $WORK

My problem is that the directory I want has a space in it:
/mnt/win98machine/My Documents
I have tried just about everything!
declare -x WORK='/mnt/win98machine/My Documents'
declare -x WORK="/mnt/win98machine/My\ Documents"
declare -x WORK='/mnt/win98machine/My\ Documents'
0
Comment
Question by:wgilster
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7160521
cd "$WORK"
0
 
LVL 1

Author Comment

by:wgilster
ID: 7160708
How do I declare the WORK variable?
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 7160719
either:
  declare -x WORK='/mnt/win98machine/My Documents'
  declare -x WORK="/mnt/win98machine/My Documents"
  declare -x WORK=/mnt/win98machine/My\ Documents
0
Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

 
LVL 1

Author Comment

by:wgilster
ID: 7160751
I thought I have tried it this way and I got:
/mnt/win98machine/My: not a valid directory

Doing other stuff, I have even gotten this message:
/mnt/win98machine/My\: not a valid directory

Anyway I'll try this out. Thanks.

0
 
LVL 40

Assisted Solution

by:jlevie
jlevie earned 100 total points
ID: 7161036
I believe the problem is probably in the usual shell function that one finds for 'cd'. Frequently it looks like:

cd () { builtin cd $1; echo $PWD; }

There are two problems with that if you want to access directories with spaces in their names. First 'builtin cd' will only see the first token (My in ths case) of the directory name. Second, even if you change the $1 to be $* it still won't work because 'builtin cd' will only act on the first part of the space separated name. The solution is to change the function to be:

cd () { builtin cd "$*"; echo $PWD;}

Then you can:

> export WORK="/mnt/win98machine/My\ Documents"
> cd $WORK
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7161344
damn, didn't think about stupid sysadmins (for distributions), who alias core UNIX programs ;-)
0
 
LVL 1

Expert Comment

by:bearware
ID: 7205930
Add this to the place you define the variable, if using bash.
shopt -s cdable_vars

Then you can do.

cd WORK
instead of
cd $WORK
This works it the variable has a space.

Another techneque would be to create a symbolic-link with no space in the path and set the variable to that.

I have had the same problem as you on several scripts. Including one that had this bug several hundred times. Please not all script writers when you dereference a variable do it like this "$Var" instead of $Var.
0
 

Expert Comment

by:CleanupPing
ID: 9076979
wgilster:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 1

Expert Comment

by:drewber
ID: 9220361
This question has been classified abandoned. I will make a recommendation to the moderators on its resolution in a week or two. I appreciate any comments that would help me to make a recommendation.
 

Unless it is clear to me that the question has been answered I will recommend delete. It is possible that a Grade less than A will be given if no expert makes a case for an A grade. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

 
If the user does not know how to close the question, the options are here:
http://www.experts-exchange.com/help/closing.jsp
 
drewber
0
 
LVL 12

Expert Comment

by:paullamhkg
ID: 10389830
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: ahoffmann {http:#7160719} & jlevie {http:#7161036}

Please leave any comments here within the next four days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

paullamhkg
EE Cleanup Volunteer
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month14 days, left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question