Solved

Environment variable question

Posted on 2002-07-17
11
193 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In this tutorial I will explain how to make squid prevent malwares in five easy steps: Squid is a caching proxy for the Web supporting HTTP, HTTPS, FTP, and more. It reduces bandwidth and improves response times by caching and reusing frequently-…
rdate is a Linux command and the network time protocol for immediate date and time setup from another machine. The clocks are synchronized by entering rdate with the -s switch (command without switch just checks the time but does not set anything). …
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 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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now