Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of the 'hash' command

Tags:

bash

hash

ffmpeg

I'm working on a small app based on ffmpeg, and I read a tutorial made for ubuntu where they advise to use the command hash on the produced executable.

I'm curious about that command, did you ever use it? For which purpose?

When I run it in my source folder, I get this (once compiled)

$ 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 on an executable file, I do not have lots of lines displayed, and nothing seems to changes in that application ?

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

When I look for the man, it just says it's a builtin function. Really useful :)

It does work (let say exist) on Linux and on MacOSX.

like image 292
Rob Avatar asked May 13 '11 05:05

Rob


People also ask

How use hash command in Unix?

On UNIX-like operating systems, a hash is a built-in command of the bash shell, which is used to list a hash table of recently executed commands. It is used for views, resets, or manually changes within the bash path hash. It keeps the locations of recently executed programs and shows them whenever we want to see it.

Why hash is used in Linux?

The Linux hash command provides information about commands on your system or those you've run recently, but what you see depends on the shell you are using. When you type “hash” on a Linux system, you could get one of two very different responses depending on the shell you are using.

What does hashed mean in Linux?

hash is a command on Unix and Unix-like operating systems that prints the location information for the commands found. The hash command has also been ported to the IBM i operating system.

What command can be used in Linux to hash files?

Generate SHA-256 Hashes for Files We can use the sha256sum command in two modes; binary and text (the default). On Linux, both modes generate the same SHA-256 hash, and so the default mode is used throughout this tutorial.


1 Answers

hash isn't actually your history; it is a bash(1) shell built-in 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).)

The guide your found may have suggested running it just to see which ffmpeg command was going to be executed by the next step; perhaps there is an ffmpeg program supplied by the distribution packaging, and they wanted to make sure the new one would be executed instead of the distro-supplied one if you just typed ffmpeg at the shell.

It seems a stretch, because it would also require having the directory containing the new ffmpeg in the PATH before the distro-provided version, and there's no guarantee of that.

like image 145
sarnold Avatar answered Sep 22 '22 14:09

sarnold