Reputation: 176
The question is in the title. I could not find anything on google, so im hoping someone here can explain this to me.
I am using debian 6.0.5 and the shell assigned to the executing user in the /etc/passwd file is /bin/bash
So, simply writing cd ~
works and brings me to the users home directory.
test -d "~/some_dir"
returns false in an if statement ( some_dir exsits )
Edit:
Sorry I should've been more clear as of why I was writing /bin/bash cd ~
instead of cd ~
: I am writing a bash script with #!/bin/bash
and the above mentioned if statement ends up in the false clause.
Upvotes: 1
Views: 24786
Reputation: 19277
If you've set $CDPATH
, you'll run into this issue.
Try CDPATH=""
before your command. Also, you can add .
to your $CDPATH:
export CDPATH=.:/path/to/anywhere
Upvotes: 0
Reputation: 46856
The options for any command line are expanded before the command is run, even for internal commands. Whatever shell you're using to run /bin/bash cd ~
is presumably interpreting the tilde literally rather than a special character that expands to your home directory.
As a test, try creating a directory by that name and see if the error goes away.
> mkdir ./~
> /bin/bash cd ~
Note that the cd
command needs to be done within your running shell to be useful. When you change the working directory of a sub-shell, and then the sub-shell exits, you'll find yourself back where you started.
UPDATE:
From within a bash script, you should be able to use the $HOME
environment variable, which should consistently contain your home directory. I'm not aware what conditions would cause tilde expansion to fail, but I've always used $HOME
.
Also, when determining whether you can change into a particular directory, you have the option of being explicit and returning useful status:
unset err
if [[ ! -d "$somedir" ]]; then
err="Can't find $somedir"
elif [[ ! -r "$somedir" ]]; then
err="Can't read $somedir"
fi
if [[ -n "$err" ]]; then
echo "$ERROR: $err" >&2
exit 1
fi
cd "$somedir"
Or, you can just try the CD and look at its results.
if ! cd "$somedir"; then
echo "ERROR: $somedir not availble"
exit 1
fi
Detailed error reports are handy, but if you don't need them, keeping your code small has advantages as well.
Upvotes: 3
Reputation: 3892
Do not use quotes ""
Example:
$ test -d ~/.aptitude
$ echo $?
0
$ test -d "~/.aptitude"
$ echo $?
1
~
is not expanded within the quotes ""
. Use $HOME
Upvotes: 2
Reputation: 58664
Assuming you did
$ /bin/bash cd ~
your shell interpreted cd
as an argument to /bin/bash
. That syntax can e.g. be used to invoke shell scripts.
What you most likely wanted was to change the current working directory. In that case all you need is
$ cd ~
Upvotes: 3