Bashscript making a variable equal a command. error command not found

15,771

Working example

#!/bin/bash
CMD="$(md5sum ../Desktop/cases/CourseworkCase/Evidence/image.dd)"
echo $CMD

Explanation

  1. To assign a variable never put a $ sign before nor spaces around the equal sign. The variable assignment in bash is like this:

    MYVAR="CONTENT"
    
  2. To create a variable with the output of a command you may use $(command). This will execute command and return its output.

  3. The output of md5sum will be like this:

    f110abe5b3cfd324c2e5128eb4733879 image.dd
    

    If do you want to isolate the md5 sum of the file name, you can use one of these lines instead:

    CMD="$(md5sum ../Desktop/cases/CourseworkCase/Evidence/image.dd | cut -d ' ' -f 1)"
    CMD=($(md5sum ../Desktop/cases/CourseworkCase/Evidence/image.dd))
    
Share:
15,771

Related videos on Youtube

octo-carrot
Author by

octo-carrot

Updated on September 18, 2022

Comments

  • octo-carrot
    octo-carrot almost 2 years

    Hi i keep getting the error

    ./imagehash.sh: line 2: =: command not found
    

    When i know that i set the $CMD variable correctly (i tried the command outside the bash script and it worked just fine)

    here's my bash script

    #!/bin/bash
     $CMD='md5sum ../Desktop/cases/CourseworkCase/Evidence/image.dd'
     echo $CMD
    

    UPDATE

    fixed the bash so theres no spaces in the $CMD variable and put '' around it but now i'm getting the error file no such file or directory i looked at the path and copied it letter for letter and its correct.

    what am i doing wrong here?

  • Nam Nguyen
    Nam Nguyen about 9 years
    also please do not forget to quote $CMD. You can use this tool to check your script: shellcheck.net
  • Nam Nguyen
    Nam Nguyen about 9 years