FWIW, the ast-open of sha1sum
(or cksum -x sha1
) which can also be the sha1sum
builtin of ksh93
if built as part of ast-open (enabled there with builtin sha1sum
or by putting /opt/ast/bin
ahead of $PATH
), or the sha1
utility as found for instance on FreeBSD, only prints the checksum when summing stdin.
So, with those implementation, you can just do:
sha1sum < thefile
sha1 < thefile
$ ./ast-open/arch/linux.i386-64/bin/ksh
$ builtin sha1sum
$ whence -a sha1sum
sha1sum is a shell builtin
sha1sum is a shell builtin version of /usr/bin/sha1sum
$ echo test | sha1sum
4e1243bd22c66e76c2ba9eddc1f91394e57f9f83
$ echo test | /usr/bin/sha1sum
4e1243bd22c66e76c2ba9eddc1f91394e57f9f83 -
openssl sha1
can also print the checksum raw, which you can convert to hex with hexdump -e '20/1 "%02x" "\n"'
or xxd -p
for instance:
openssl sha1 -binary -- "$file" | xxd -p
The GNU implementation of sha1sum
outputs <thehexsum> -
(the second space possibly replaced with *
to indicate binary mode on MSDOS-like systems), same for the shasum
utility shipped with perl
. openssl sha1
outputs (stdin)= <thehexsum>
, openssl sha1 -r
(-r
meant to emulate GNU coreutils format) outputs <thehexsum> *stdin
So, as already said, with those, you'd have to extract the checksum from that:
sha1sum < "$file" | awk '{print $1}'
(with set -o pipefail
beforehand where supported to preserve sha1sum
(or file input redirection) failure if any)
Or, in POSIX-like shells:
sum=$(sha1sum < "$file") && sum=${sum%%[[:space:]]*}
[[:space:]]*
can also be replaced with [![:xdigit:]]*
to remove everything starting with the first character that is not a hexadecimal digit.
shasum
is a perl script that has command line options modelled on GNU coreutils. – Gilles 'SO- stop being evil' Aug 07 '22 at 08:28pipefail
will be a standardsh
option in the next versions of the standard and is already supported by manysh
implementations. – Stéphane Chazelas Aug 07 '22 at 08:32