Skip to main content
1 of 2
Vlastimil Burián
  • 31.3k
  • 67
  • 211
  • 359

Replacing trailing text POSIX-ly

As I did not find a direct way to supply the file name to the pv (man page), (except for a strange -N switch, which acts as a prefix, rather than the file name itself), I would need to manually edit the end of text which sha512sum or any other shaXsum for that matter outputs over pipe like:

pv -W "$file" | sha512sum -b 

I store it in a variable sha_output, but I wanted to give you purer example, the raw code is:

sha_output=$( pv -W "$file" | sha512sum -b ) 

Example hash sum text output:

2a19f5852ba8f76bd5a67db18539d609baaf3888b27a57181564db01ef6c16812c60e306973edc059221f61bf2ad9f4b4ef5ff09bbcef98e74a27971c67bdc18 *- 

Desired output:

2a19f5852ba8f76bd5a67db18539d609baaf3888b27a57181564db01ef6c16812c60e306973edc059221f61bf2ad9f4b4ef5ff09bbcef98e74a27971c67bdc18 linuxmint-19.2-cinnamon-64bit.iso 

(Yes, 2 spaces in between the hash and the file name).

Finally, let us suppose the file name is stored in a variable called file_name.


Solutions must not include any Bashisms, only portable (POSIX) solutions, please.

With the help of this answer, I put together the following:

printf '%s\n' "${sha_output//\*-/$file_name}" 

which surprisingly works (somewhat) under Bash, but Dash errors out with message:

bad substitution 

If this is not possible to do with brace / parameter expansion, please remove the tag, thank you.

Vlastimil Burián
  • 31.3k
  • 67
  • 211
  • 359