Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read result of sha256sum into a bash variable [duplicate]

Tags:

bash

For the following execution and result of sha256sum

~/HydroGuardFW/hw_1_5/Debug$ sha256sum debug_2.0.5.hex
34c977f0df3e90f9a4b36da3cd39fdccf3cd1123c563894b3a3378770a19fc6d      debug_2.0.5.hex

The output will be in two parts, the sha256 and the echo of the file name of which the sha256 sum was calculated. How do you grab the first part part of the output, which is the sha256 into a variable so it can be placed into a file using bash script.

like image 567
user1135541 Avatar asked Oct 11 '16 13:10

user1135541


People also ask

How do I read a text file in bash?

Syntax: Read file line by line on a Bash Unix & Linux shell The syntax is as follows for bash, ksh, zsh, and all other shells to read a file line by line: while read -r line; do COMMAND; done < input. file. The -r option passed to read command prevents backslash escapes from being interpreted.


1 Answers

You don't need store it in a variable. You can directly redirect it to the file as well.

sha256sum  debug_2.0.5.hex | awk '{print $1}' > dsl

If you do need to store it in a variable for some other purpose then:

read -r shm_id rest <<<"$(sha256sum  scr)"
echo $shm_id > dsl

or

shm_id=$(sha256sum  scr | awk '{print $1}')
like image 176
P.P Avatar answered Sep 21 '22 12:09

P.P