7

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.

user1135541
  • 1,781
  • 3
  • 19
  • 41

1 Answers1

18

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}')
P.P
  • 117,907
  • 20
  • 175
  • 238
  • 2
    `shm_id=$(sha256sum filename | awk '{print $1}')` was all that was needed to answer this question. – lasec0203 Sep 02 '17 at 04:10
  • @lasec0203 That's related to the question. The question was tagged `bash` so I thought it would be a good idea to show how to do it in bash itself. `shm_id=$(sha256sum scr | awk '{print $1}')` is *simple* but `sha256sum debug_2.0.5.hex | awk '{print $1}' > dsl` is *advanced*?! Your reasoning is beyond my comprehension, I'll leave it at that. – P.P Sep 04 '17 at 08:20
  • 3
    Maybe `sha256sum file | cut -d' ' -f1` is better since the chance of missing `awk` is higher. – AleXoundOS Mar 22 '19 at 20:00