J K
J K

Reputation: 495

bash - cd command not working?

I've somehow managed to screw up bash while fiddling with the $PATH variable in my bash_profile (I think...). All I did, as far as I can remember, was add a directory to the $PATH variable. Please HELP!

Here's what I get when I cd into various directories

my-MacBook-Pro:~ myuser$ cd .rvm
-bash: dirname: command not found
-bash: find: command not found
my-MacBook-Pro:.rvm myuser$ cd
-bash: find: command not found

And here's what happens when I try to get into my .bash_profile to undo whatever it is that I did...

my-MacBook-Pro:~ myuser$ emacs .bash_profile
-bash: emacs: command not found

my-MacBook-Pro:~ myuser$ sudo emacs .bash_profile
-bash: sudo: command not found

Any help would be massively appreciated. I'm completely screwed until I can get bash working normally again!

Upvotes: 1

Views: 13255

Answers (4)

David W.
David W.

Reputation: 107030

When you do a cd, you're getting a bunch of other things. Since you're using BASH there are are two possible issues:

  • You have PROMPT_COMMAND defined. Try to undefining it:

    $ unset PROMPT_COMMAND

  • There's an alias of the cd command: This was quite common in Kornshell where you don't have the nice backslashed characters you could put into your prompt string. If you wanted your prompt to have the name of your directory in it.

You had to do something like this:

function _cd
{
    logname="$(logname)"
    hostname="$(hostname)"
    directory="$1"
    pattern="$2"

    if [ "$pattern" ]        #This is a substitution!
    then
        \cd "$directory" "$pattern"
    elif [ "$directory" ]
    then
        \cd "$directory"
    else
        \cd
    fi
    directory=$PWD
    shortName=${directory#$HOME}

    if [ "$shortName" = "" ]
    then
        prompt="~$logname"
    elif [ "$shortName" = "$directory" ]
    then
        prompt="$directory"
    else
        prompt="~$shortName"
    fi
    title="$logname@$hostname:$prompt"
    PS1="$title
$ "
}

alias cd="_cd"

Ugly isn't it? You don't have to go through all of that for BASH, but this does work in BASH too, and I've seen places where this was done either out of ignorance of inertia.

Try this:

$ type cd

You'll either get

$type cd
cd is a shell builtin

or you'll get

$ type cd 
cd is an alias for ....

As for your updating of $PATH, you probably forgot to put $PATH back in the new definition, or quotation marks because someone has a directory name with a space in it. Your PATH setting should look like this:

PATH="/my/directory:$PATH"

Some people say it should be:

PATH="$PATH:/my/directory"

Upvotes: 1

Matthias
Matthias

Reputation: 8180

I guess, that you have defined $PROMPT_COMMAND (maybe in .bashrc) in a way that uses dirname and find. That would explain the behavior of cd.

The find command is by default in /usr/bin/find. Thus, you can use it to find the locations of your imprtant commands and reconstruct you path information.

Upvotes: 0

CoffeeRain
CoffeeRain

Reputation: 4522

The $PATH variable tells the shell where to look for commands. If you just bypass that by telling it the full path, it should work. Try /usr/bin/emacs .bash_profile.

Upvotes: 1

Erik Ekman
Erik Ekman

Reputation: 2066

/usr/bin/emacs .bash_profile or similar should work when the PATH is broken.

Upvotes: 2

Related Questions