Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

Environment variable question

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
wgilster
Asked:
wgilster
2 Solutions
 
ahoffmannCommented:
cd "$WORK"
0
 
wgilsterAuthor Commented:
How do I declare the WORK variable?
0
 
ahoffmannCommented:
either:
  declare -x WORK='/mnt/win98machine/My Documents'
  declare -x WORK="/mnt/win98machine/My Documents"
  declare -x WORK=/mnt/win98machine/My\ Documents
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
wgilsterAuthor Commented:
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
 
jlevieCommented:
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
 
ahoffmannCommented:
damn, didn't think about stupid sysadmins (for distributions), who alias core UNIX programs ;-)
0
 
bearwareCommented:
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
 
CleanupPingCommented:
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
 
drewberCommented:
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
 
paullamhkgCommented:
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

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Tackle projects and never again get stuck behind a technical roadblock.
Join Now