31

I know this question has been asked numerous times, but I still could not find any good solution. Hence, asking it again if anyone can help !!

I am trying to change a my working directory inside a shell script with help of a variable. But I get " No such file or directory" everytime.

#!/bin/bash
echo ${RED_INSTANCE_NAME}   <-- This correctly displays the directory name
cd $RED_INSTANCE_NAME       <-- This line gives the error

Now, when I try to give the actually directory name instead of using the variable, shell changes the directory without issues

cd test  <-- No error

Does anyone knows what can be the issue here ? Please help !!

Vivek
  • 957
  • 4
  • 12
  • 18
  • How are you setting the value of `RED_INSTANCE_NAME`? – chepner Oct 09 '13 at 19:58
  • 3
    Try saying `cd "${RED_INSTANCE_NAME}"` – devnull Oct 09 '13 at 19:58
  • If you think this question has already been asked, it might be good to link to those similar questions and explain why the answers are not right for you. – Adrian Ratnapala Oct 09 '13 at 19:59
  • you could use the echo command like this to see if any other character is in the var value: echo "'${RED_INSTANCE_NAME}'" – LMC Oct 09 '13 at 21:05
  • I'd also drop a `pwd` into your script to validate that the script thinks it's in the right directory; and possible an `ls -F` as well. What you're doing should work, so it will either be spaces in the directory name, confusion over case, leading/trailing white space (or other unprintable character), or the script isn't running from where you think it's running from. – Chris J Oct 10 '13 at 18:33

6 Answers6

34

You variable contains a carriage return. Try saying:

cd $(echo $RED_INSTANCE_NAME | tr -d '\r')

and it should work. In order to remove the CR from the variable you can say:

RED_INSTANCE_NAME=$(echo $RED_INSTANCE_NAME | tr -d '\r')

The following would illustrate the issue:

$ mkdir abc
$ foo=abc$'\r'
$ echo "${foo}"
abc
$ cd "${foo}"
: No such file or directory
$ echo $foo | od -x
0000000 6261 0d63 000a
0000005
$ echo $foo | tr -d '\r' | od -x
0000000 6261 0a63
0000004
$ echo $'\r' | od -x
0000000 0a0d
0000002
devnull
  • 118,548
  • 33
  • 236
  • 227
29

One way to encounter your described problem is to have a tilde (~) in the variable name. Use the absolute path or $HOME variable instead. Note that using $HOME will require double quotations.

# doesn't work
$ vartilde='~/'
$ cd $vartilde
-bash: cd: ~: No such file or directory

# works
$ varfullpath='/Users/recurvirostridae'
$ cd $varfullpath

# works
$ varwithhome="$HOME"
$ cd $varwithhome
trailing slash
  • 881
  • 11
  • 13
11

Try

cd "$RED_INSTANCE_NAME"

Also, make sure the path makes sense to the current directory where cd command is executed.

Alexander L. Belikoff
  • 5,698
  • 1
  • 25
  • 31
  • 1
    Thanks for answering, but I tried this way too. But no help. it still gives the error !! And yes, I am sure that I am executing this command in correct directory – Vivek Oct 09 '13 at 20:07
  • What is the error you are seeing? Try running via "bash -x" to see what it executes. – Alexander L. Belikoff Oct 09 '13 at 20:11
5

I ran into a different issue. My "cd $newDir" was failing because I added logging into my script. Apparently if you add a pipe to any cd command it does nothing or gets gobbled up.

Wasted 3 hours figuring that out.

newDir=$oldDir/more/dirs/

cd $newDir #works

cd $newDir | tee -a log #does nothing

cd $newdir | echo hi    #does nothing

So cd with any pipe does nothing. No idea why cd fails. The pipe means finish what command you doing then feed any output to next command. This is on RHEL 7.
I was trying to log all my commands and hit this nice error. Figured I'd post it in case anyone else hits it.

N3R4ZZuRR0
  • 2,400
  • 4
  • 18
  • 32
david jimenez
  • 51
  • 1
  • 2
2

You can check for carriage returns, ANSI escapes and other special characters with

cat -v <<< "$RED_INSTANCE_NAME"

This will show all the characters that echo $RED_INSTANCE_NAME would just hide or ignore.

In particular, if your error message is : No such file or directory as opposed to bash: cd: yourdir: No such file or directory, it means you have a carriage return at the end of your variable, probably from reading it from a DOS formatted file.

that other guy
  • 116,971
  • 11
  • 170
  • 194
1

I don't know what is going wrong for you, but I can offer one piece of general advice:

cd "$RED_INSTANCE_NAME"       # Quote the string in case it has spaces.error

You should nearly always put the "$VARIABLE" in quotes. This will protect from surprises when the value of the variable contains funny stuff (like spaces).

Adrian Ratnapala
  • 5,485
  • 2
  • 29
  • 39
  • Thanks for answering, but I tried this way too. But no help. it still gives the error !! – Vivek Oct 09 '13 at 20:08