Using the hash Command

I am working on a small application based on ffmpeg and I read a tutorial made for ubuntu where they advise using the hash command on releasing the executable.

I am interested in this command, have you ever used it? For what?

When I run it in my source folder, I get this (after compilation)

 $ hash hits command 1 /usr/bin/strip 1 /usr/local/bin/ffmpeg 1 /usr/bin/svn 4 /usr/local/bin/brew 2 /usr/bin/git 1 /bin/rm 1 /bin/cat 1 /usr/bin/ld 1 /bin/sh 4 /usr/bin/man 5 /usr/bin/make 4 /usr/bin/otool 15 /bin/ls 6 /usr/bin/open 2 /usr/bin/clear 

Looks like a summary of my bash_history ...

When I run it in an executable file, I do not have many lines, and nothing changes in this application?

 $ md5 ffserver MD5 (ffserver) = 2beac612e5efd6ee4a827ae0893ee338 $ hash ffserver $ md5 ffserver MD5 (ffserver) = 2beac612e5efd6ee4a827ae0893ee338 

When I search for a person, he simply says that it is a built-in function. Really helpful :)

It works (even if it exists) on Linux and MacOSX .

Thank you for your help, I probably will find out something today :)

+6
source share
2 answers

hash is not really your story; This is the bash(1) built-in shell that maintains a hash table of recently executed programs:

  Bash uses a hash table to remember the full pathnames of executable files (see hash under SHELL BUILTIN COMMANDS below). A full search of the directories in PATH is performed only if the command is not found in the hash table. 

(From bash(1) .)

In the manual you found, you might be prompted to run it to see which ffmpeg command should have been executed in the next step; perhaps there is a ffmpeg program supplied with the distribution package, and they wanted to make sure that the new one would be executed instead of the distribution package if you just typed ffmpeg in the shell.

This seems to be stretched because it will also require the presence of a directory containing the new ffmpeg in PATH in front of the version provided by the distribution, and there is no guarantee.

+8
source

If you use commands that cannot be installed on the system, check their availability and tell the user what is missing. From Scripts with Style

Example:

 NEEDED_COMMANDS="sed awk lsof who" missing_counter=0 for needed_command in $NEEDED_COMMANDS; do if ! hash "$needed_command" >/dev/null 2>&1; then printf "Command not found in PATH: %s\n" "$needed_command" >&2 ((missing_counter++)) fi done if ((missing_counter > 0)); then printf "Minimum %d commands are missing in PATH, aborting" "$missing_counter" >&2 exit 1 fi 
+6
source

Source: https://habr.com/ru/post/887992/


All Articles