skip to Main Content

I already have a pre-installed gcc in debian, and I also have compiled another gcc successfully, which is installed in /root/tools/.But after that, I typed type gcc, it showed gcc is hashed (/bin/gcc) . What does this mean ?

My bash PATH variable : PATH=/root/tools/bin:/bin:/usr/bin

2

Answers


  1. hashed means that the shell knows where it is. When you run a command like gcc the first time, the shell goes and finds it in the path. The second time, the shell doesn’t go look for it, because the shell has remembered where it is. This is called hashing, probably because it uses a hash table internally.

    If you want the shell to forget its remembered locations, use the command rehash. This is useful if you install a new program somewhere, but the shell won’t find it, because it’s remembering its old location.

    Edited to add: You can also use hash -l to see what the shell has hashed.

    $ hash -l
    builtin hash -p /usr/bin/git git
    builtin hash -p /home/alester/bin/dirtysmoke dirtysmoke
    builtin hash -p /usr/bin/chmod chmod
    builtin hash -p /usr/local/bin/exa exa
    builtin hash -p /usr/bin/ssh-add ssh-add
    builtin hash -p /usr/bin/mkdir mkdir
    builtin hash -p /usr/bin/man man
    
    Login or Signup to reply.
  2. In the addition to Andy’s answer here is the list of some examples.
    They show how bash adds file paths to its hash:

    $ hash -l
    hash: hash table empty
    
    $ env
    
    $ hash -l
    builtin hash -p /usr/bin/env env
    
    $ gcc
    
    $ hash -l
    builtin hash -p /usr/bin/gcc gcc
    builtin hash -p /usr/bin/env env
    

    Part II. If you want to see the path to your program i suggest to use "$type -p" :

    $ type gcc
    gcc is hashed (/usr/bin/gcc)
    $ type -p gcc
    /usr/bin/gcc
    

    -p key description:

    If the -p option is used, type either returns the name of the disk
    file that would be executed if name were specified as a command name,
    or nothing if “type -t name” would not return file

    Sometimes -p key is not enough. -a key will help:

    $ type -p printf
    $ type -a printf
    printf is a shell builtin
    printf is /usr/bin/printf
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search