views:

23

answers:

2

Here's a very simple, probably duplicate (sorry - I have tried searching) shell scripting question.

What's the correct syntax to set DATA_DIR as a subdirectory of WDM_DIR, in the script below? (Line 2.)

WDM_DIR='/Users/ap257/wdm/wdm'
DATA_DIR=$WDM_DIR+/wdm/pylons_data/getdata/
cd $DATA_DIR

The point is that people can change WDM_DIR to whatever path is right for their system, but DATA_DIR is always in the same place relative to it.

A: 

The easy way is:

DATA_DIR="${WDM_DIR}/wdm/pylons_data/getdata/"
Drakosha
+1  A: 

Here is the correct syntax

WDM_DIR=/Users/ap257/wdm/wdm
DATA_DIR=${WDM_DIR}/wdm/pylons_data/getdata/
cd -- "$DATA_DIR"

Well the syntax may vary a bit depending on what shell you are using.

Gopi
The last line should be `cd -- "$DATA_DIR"`. The `--` is in case `$WDM_DIR` starts with a `-` (not a problem if you require an absolute path). The quotes are in case `$WDM_DIR` contains a special character such as whitespace. Rule 1 of shell scripting: **Always put double quotes around variable substitutions**.
Gilles
@Gilles Thank you for the inputs. I have updated my answer with your suggestions.
Gopi