Solved

Environment variable question

Posted on 2002-07-17
11
197 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
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 25 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 25 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

749 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