# cd command doesn't work through variables

**URL:** <https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153>\
**Category:** Shell Programming and Scripting\
**Created:** [May 2, 2009, 12:44am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153 "2009-05-02T00:44:07Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![vipinchauhan222](https://community.unix.com/letter_avatar/vipinchauhan222/32/6_449c533f586939b6e5f246632a46421f.png) [@vipinchauhan222](https://community.unix.com/u/vipinchauhan222)\
**Post date:** [May 2, 2009, 12:44am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/1 "2009-05-02T00:44:07Z")

</div>

Hi....  
cd command is not working when dual string drive/volume name is passed to cd through variables.......  
For Ex....  
y=/Volumes/Backup\ vipin/  
cd $y

the above command gives error.......  
anyone with a genuine solution ?

---

<div class="post-metadata">

**Author:** ![devtakh](https://community.unix.com/letter_avatar/devtakh/32/6_449c533f586939b6e5f246632a46421f.png) [@devtakh](https://community.unix.com/u/devtakh)\
**Post date:** [May 2, 2009, 12:51am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/2 "2009-05-02T00:51:14Z")

</div>

try:  
make sure the PATH is correct. The shell has to evaluate the value of y first before it cn cd.

```nohighlight
cd ${y}

```

cheers,  
Devaraj Takhellambam

---

<div class="post-metadata">

**Author:** ![vipinchauhan222](https://community.unix.com/letter_avatar/vipinchauhan222/32/6_449c533f586939b6e5f246632a46421f.png) [@vipinchauhan222](https://community.unix.com/u/vipinchauhan222)\
**Post date:** [May 2, 2009, 12:58am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/3 "2009-05-02T00:58:53Z")

</div>

it doens't work in case of dual word volume name........  
that space between the two word is the culprit.....

---

<div class="post-metadata">

**Author:** ![vipinchauhan222](https://community.unix.com/letter_avatar/vipinchauhan222/32/6_449c533f586939b6e5f246632a46421f.png) [@vipinchauhan222](https://community.unix.com/u/vipinchauhan222)\
**Post date:** [May 2, 2009, 1:24am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/4 "2009-05-02T01:24:40Z")

</div>

even though the shell evaluate the value of variable correctly still cd doesnt work ,gives error because of the space between two words

---

<div class="post-metadata">

**Author:** ![devtakh](https://community.unix.com/letter_avatar/devtakh/32/6_449c533f586939b6e5f246632a46421f.png) [@devtakh](https://community.unix.com/u/devtakh)\
**Post date:** [May 2, 2009, 1:34am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/5 "2009-05-02T01:34:54Z")

</div>

> [@vipinchauhan222](#):
>
> even though the shell evaluate the value of variable correctly still cd doesnt work ,gives error because of the space between two words

What is the actual directory path? Can you do a direct CD to the PATH?

---

<div class="post-metadata">

**Author:** ![vipinchauhan222](https://community.unix.com/letter_avatar/vipinchauhan222/32/6_449c533f586939b6e5f246632a46421f.png) [@vipinchauhan222](https://community.unix.com/u/vipinchauhan222)\
**Post date:** [May 2, 2009, 1:40am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/6 "2009-05-02T01:40:03Z")

</div>

yes the actually cd to the path is possible but it doesn't work when the value is passed through a variable........  
for Ex....  
cd '/Volumes/Backup vipin/' is working  
But  
y=''/Volumes/Backup vipin/'"  
echo $y  
'/Volumes/Backup vipin/'  
cd $y  
-sh: cd: '/Volumes/Backup: No such file or directory

doesn't work...

---

<div class="post-metadata">

**Author:** ![manish6725](https://community.unix.com/letter_avatar/manish6725/32/6_449c533f586939b6e5f246632a46421f.png) [@manish6725](https://community.unix.com/u/manish6725)\
**Post date:** [May 2, 2009, 1:43am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/7 "2009-05-02T01:43:43Z")

</div>

I am also new to shell scripting can you tell me ..  
why are you using "\" in /Volumes/Backup\ vipin/

---

<div class="post-metadata">

**Author:** ![devtakh](https://community.unix.com/letter_avatar/devtakh/32/6_449c533f586939b6e5f246632a46421f.png) [@devtakh](https://community.unix.com/u/devtakh)\
**Post date:** [May 2, 2009, 1:44am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/8 "2009-05-02T01:44:46Z")

</div>

Now I understand what you are trying to do ..put it in the quotes as there is a space.

```nohighlight
y='/Volumes/Backup vipin'
cd "${y}"

```

cheers,  
Devaraj Takhellambam

---

<div class="post-metadata">

**Author:** ![vipinchauhan222](https://community.unix.com/letter_avatar/vipinchauhan222/32/6_449c533f586939b6e5f246632a46421f.png) [@vipinchauhan222](https://community.unix.com/u/vipinchauhan222)\
**Post date:** [May 2, 2009, 1:55am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/9 "2009-05-02T01:55:37Z")

</div>

"\" is used because the next special charactor after "\" can be considered as a normal charactor and a part of the string

---

<div class="post-metadata">

**Author:** ![robsonde](https://community.unix.com/letter_avatar/robsonde/32/6_449c533f586939b6e5f246632a46421f.png) [@robsonde](https://community.unix.com/u/robsonde)\
**Post date:** [May 2, 2009, 1:55am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/10 "2009-05-02T01:55:37Z")

</div>

try this....

```nohighlight
y="/Volumes/Backup\ vipin/"
cd "$y"

```

---

<div class="post-metadata">

**Author:** ![vipinchauhan222](https://community.unix.com/letter_avatar/vipinchauhan222/32/6_449c533f586939b6e5f246632a46421f.png) [@vipinchauhan222](https://community.unix.com/u/vipinchauhan222)\
**Post date:** [May 2, 2009, 1:57am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/11 "2009-05-02T01:57:33Z")

</div>

your idea doesn't work.here is the error message.......  
-sh: cd: /Volumes/Backup\ vipin/: No such file or directory

---

<div class="post-metadata">

**Author:** ![vipinchauhan222](https://community.unix.com/letter_avatar/vipinchauhan222/32/6_449c533f586939b6e5f246632a46421f.png) [@vipinchauhan222](https://community.unix.com/u/vipinchauhan222)\
**Post date:** [May 2, 2009, 1:57am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/12 "2009-05-02T01:57:51Z")

</div>

your idea doesn't work.here is the error message.......  
-sh: cd: /Volumes/Backup\ vipin/: No such file or directory

---

<div class="post-metadata">

**Author:** ![robsonde](https://community.unix.com/letter_avatar/robsonde/32/6_449c533f586939b6e5f246632a46421f.png) [@robsonde](https://community.unix.com/u/robsonde)\
**Post date:** [May 2, 2009, 2:04am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/13 "2009-05-02T02:04:01Z")

</div>

try dropping the escaping slash.....

```nohighlight
y="/Volumes/Backup vipin/"
cd "$y"

```

---

<div class="post-metadata">

**Author:** ![devtakh](https://community.unix.com/letter_avatar/devtakh/32/6_449c533f586939b6e5f246632a46421f.png) [@devtakh](https://community.unix.com/u/devtakh)\
**Post date:** [May 2, 2009, 2:09am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/14 "2009-05-02T02:09:39Z")

</div>

Did you try this

```nohighlight
y='/Volumes/Backup vipin'
cd "${y}"

```

cheers,  
Devaraj Takhellambam

---

<div class="post-metadata">

**Author:** ![robsonde](https://community.unix.com/letter_avatar/robsonde/32/6_449c533f586939b6e5f246632a46421f.png) [@robsonde](https://community.unix.com/u/robsonde)\
**Post date:** [May 2, 2009, 2:11am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/15 "2009-05-02T02:11:31Z")

</div>

BTW, which shell are you using???

it can / will act differently in each shell.

---

<div class="post-metadata">

**Author:** ![vipinchauhan222](https://community.unix.com/letter_avatar/vipinchauhan222/32/6_449c533f586939b6e5f246632a46421f.png) [@vipinchauhan222](https://community.unix.com/u/vipinchauhan222)\
**Post date:** [May 2, 2009, 2:12am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/16 "2009-05-02T02:12:39Z")

</div>

ya it works............  
nice grey matter......

---

<div class="post-metadata">

**Author:** ![devtakh](https://community.unix.com/letter_avatar/devtakh/32/6_449c533f586939b6e5f246632a46421f.png) [@devtakh](https://community.unix.com/u/devtakh)\
**Post date:** [May 2, 2009, 3:22am UTC](https://community.unix.com/t/cd-command-doesnt-work-through-variables/239153/17 "2009-05-02T03:22:29Z")

</div>

> [@vipinchauhan222](#):
>
> ya it works............  
> nice grey matter......

Good. The quotes are confusing at times. So the codes are put into the grey code section for easier visibility.

cheers,  
Devaraj Takhellambam
