Solved

Environment variable question

Posted on 2002-07-17
11
195 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Postfix issues with spam/auth attempts under NAT 9 86
App holding yum lock unable to update my rpm package 1 54
How to learn Linux? 10 61
winscp where are logs stored 3 36
How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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…

773 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