Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is a simple script which should let me go to to directory of my choice, but its not working as intended.


showMenu() {
    echo "1) ocaml"
    echo "2) python"
    echo "3) csharp"

while [ 1 ]
    read CHOICE
    case $CHOICE in
            "cd ~/projects/practice/ocaml"
            "cd ~/projects/practice/python"
            "cd ~/projects/practice/csharp"

I have all the necessary folders in place:

(2) [01:19 PM] ls projects/practice/
csharp/  ocaml/   python/  scripts/ web/     


(2) [01:19 PM] choice 
1) ocaml
2) python
3) csharp
/home/nanda/bin/choice: line 15: cd ~/projects/practice/ocaml: No such file or directory
1) ocaml
2) python
3) csharp

The script can't find the directory apparently.

I feel that I need to put the cd command, under each option, in some kind of substitution (with ${} or {}), but I am not sure how it could be done.

Also, do I need to add a break statement after each cd command?

I should do . choice to make it work as intended. Better yet, I put in an alias like this and it worked:

alias choice='. choice'


share|improve this question
Can you confirm that you have your projects directory directly inside of your home directory? It is not clear from you ls projects/practice/ output as we don't know what is your current directory when you run this. Could you change it to ls ~/projects/practice/ output? Also, can you confirm that you run this script as the same user as when running ls command? And maybe you could check if replacing ~/ with full directory path fixes the problem? – Krzysztof Adamski Oct 7 '12 at 8:30
Yes, I confirm that the projects directory is inside home. I did ls projects/practice because I was already inside home. The script choice is in ~/bin. – Nanda Oct 7 '12 at 8:48
Then my answer below should be correct (and Julians answer too, as it's the same) and should fix your problem. – Krzysztof Adamski Oct 7 '12 at 8:52
I am not sure exactly you are trying to make given the lack of details on that but I have a small recommendation if it happens to apply. I can tell that this has to do with some kind of programming and using different languages and it may be useful to add the option to manually key the dir to avoid leaving the menu. – Joe Oct 7 '12 at 10:03
Such as "4") echo "Enter Custom DIR: " read CUSDIR cd $CUSDIR ;; – Joe Oct 7 '12 at 10:11
up vote 4 down vote accepted

You have your commands inside of quotation marks. This makes your shell treat cd ~/projects/practice/ocaml as a command name. As you don't have this command, it prints this error message. Files can contain space in their name so you could have command that has space in name. If you would like to run it, one way of doing it would be to use quotation marks as you did.

But you want your shell to run command cd and give it the path as argument. Just remote quotation marks and it should be working fine.

Whether you should or shouldn't use break statement in your script depends on what you want to do after changing directory. If you want to leave the loop then you should use it.

Note that your cd command in shell script won't change current directory after running this script. It will only affect current directory inside of the script (unless you use source instead of running it) since when you run the script, new process is created and cd only affects current directory in current and child processes, not in parent one.

share|improve this answer
I removed the quotes around "cd ~/projects/practice/ocaml". Now if I enter any option, it is not throwing any error, but its not going any where either. I mean I am still stuck in the while loop. – Nanda Oct 7 '12 at 8:52
@Nanda: Your while loop is endless so you won't go anyware. You can add pwd command at the end of the loop to see that the current directory has changed. Or you can use break statement to leave the loop (so yes, you should use break statement for this case). – Krzysztof Adamski Oct 7 '12 at 8:55
I did add the pwd command, it is indeed going into the intended directory, but it is still in the loop. So, if I break the loop with ^C, I am still where I was originally. So I put break;; and it came out of the loop. But to my surprise, when I do pwd after coming out of the loop, I still am where I started the script from. – Nanda Oct 7 '12 at 9:01
@Nanda: Read the note at the end of my answer. – Krzysztof Adamski Oct 7 '12 at 9:02
After sourcing, it worked. So basically all I have to do is either source choice or . choice. Thanks Krzysztof. – Nanda Oct 7 '12 at 9:12

This situation is exactly what the shell's select statement is for:

select choice in ocaml python csharp; do
    case $choice in
        ocaml|python|csharp) break ;;
echo cd ~/projects/practice/$choice

Slightly DRYer:

choices=(ocaml python csharp)
select choice in "${choices[@]}"; do
    [[ " ${choices[*]} " == *" $choice "* ]] && break
echo cd ~/projects/practice/$choice
share|improve this answer
+1; slightly OT: DRY? – jasonwryan Oct 7 '12 at 18:57
Don't Repeat Yourself – glenn jackman Oct 8 '12 at 2:02
Thank you: that makes sense :) – jasonwryan Oct 8 '12 at 2:10

You have quotes around the cd command. Remove those and you should be fine.

"cd ~/projects/practice/ocaml"

says run that command - the whole string, so it will look for such a command, including the space and the ~ and everything.

You want the command to be cd with an argument of the directory.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.