1

I am writing a shell script to enter multiple folders. I am currently storing the name of folder in a shell variable as so path="October\ @012/". If I do cd $path I receive the error bash: cd: October\: No such file or directory

What am I doing wrong?

0

2 Answers 2

7

This is the (principal) function of double quotes, and it's true in csh and *sh shells.

cd "$TARGET" 

should do it.

Shell variables are expanded within "..." (unlike within '...'), but the quoted text is regarded as a single argument when the shell parses the command line to construct the strings which are passed to the program.

For example:

% ls -F October @012/ % TARGET="October @012" % cd $TARGET bash: cd: October: No such file or directory % cd "$TARGET" % pwd /tmp/t/October @012 % 

Simple!

What you're doing wrong in your initial example is escaping the space inside the quotes. The space doesn't have to be escaped twice, and because this redundant \ is appearing inside the quotes, it just inserts a backslash into the TARGET variable. For example:

% TARGET="October\ @012" # wrong! % ls October @012/ % cd $TARGET bash: cd: October\: No such file or directory % cd "$TARGET" bash: cd: October\ @012: No such file or directory % 

This setting of TARGET would only work if the directory were named October\ @012, with a backslash in it (not recommended!):

% mkdir October\\\ @012 % ls -F October\ @012/ % cd "$TARGET" % pwd /tmp/t/October\ @012 % 

(EDITED to add example)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for pointing out my folly in answering questions in the middle of the night during lack of sleep. You of course are right and I edited my answer before I saw that you had correctly answered the question.
Please help me with this:I am writing a shell script to enter multiple folders. I am currently storing the name of folder in a shell variable as so path="October\ @012/". If I do cd $path I receive the error bash: cd: October\: No such file or directory What am I doing wrong?
Thank You Norman, This worked and also thanks for the explanation, it helped a lot
1

EDIT: I originally had written to recommend using curly braces. I wrote this when I had awoke in the middle of the night and have modified my answer.

First off it really depends on which shell you are writing your script in. If it is bash then you could try using quotation marks around your variable name:

TARGET="October @012" cd "$TARGET" 

This may work in other shells as well. I would suggest you try it.

EDIT:

On re-examining this it appears you are escaping the wrong part in your expression. Try this:

path="October \@012" cd "$path" 

6 Comments

Thank you for your reply, it is taking October and @012 as two different folders. But the folder name is "October @012" (space separated), I also tried ur method but got this error bash: cd: October: No such file or directory
No, that's not the function of the ${...} syntax. Here, the ${TARGET} would be expanded to cd October @012, and then split at spaces into words, exactly as before. ${...} is for the case where the expansion might get confused (for example $TARGETX vs ${TARGET}X, or where you want to do other processing (compare echo $foo, where $foo is undefined, vs echo ${foo:-bar})
That's what I get for answering questions when I wake in the middle of the night and can't seem to sleep. When I have used this in the past I have been solving the problem you Norman addressed, along with wrapping in quotes "" to address the OPs problem.
Please help me with this issue: I am writing a shell script to enter multiple folders. I am currently storing the name of folder in a shell variable as so path="October\ @012/". If I do cd $path I receive the error bash: cd: October\: No such file or directory Its urgent
Okay, so can you not use cd "$path" as both I and Norman have mentioned and accomplish what you are wanting?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.