Bashscript making a variable equal a command. error command not found
Working example
#!/bin/bash
CMD="$(md5sum ../Desktop/cases/CourseworkCase/Evidence/image.dd)"
echo $CMD
Explanation
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"
To create a variable with the output of a command you may use
$(command)
. This will executecommand
and return its output.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))
Related videos on Youtube
octo-carrot
Updated on September 18, 2022Comments
-
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 fileno 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 about 9 yearsalso please do not forget to quote $CMD. You can use this tool to check your script: shellcheck.net
-
Nam Nguyen about 9 years