Variables Declared in Shell Script Not Being Recognized as a Variable and Instead a Command [Duplicate]
Here Is the Script That I Wrote: #! /Bin/Bash Directory1 = ~/Path/to/Directory/ Directory2 = ~/Path/to/Directory2/ Diff -R $Directory1 $Directory2 || Echo...
Here is the script that I wrote:
#!/bin/bash
directory1 = ~/path/to/directory/
directory2 = ~/path/to/directory2/
diff -r $directory1 $directory2 || echo "files are different"
And here is the output/error message that appears:
./compare.sh: line 2: directory1: command not found
./compare.sh: line 3: directory2: command not found
diff: missing operand after `-r'
diff: Try `diff --help' for more information.
files are different
I know that there is a problem in a way that I defined directory1 and directory2, but I don't exactly know what is wrong. Any help would be appreciated. Thanks!
Spaces are used as delimiters when assigning variables in bash; you should remove them, otherwise your variables don't exist (i.e. are empty) so the line
directory1 = ~/path/to/directory/
actually means "call program directory1 with arguments = and ~/path/to/directory, hence the " command not found".
Similarly, your call to
diff -r $directory1 $directory
is equivalent to
diff -r
which is indeed missing parameters.
You might also want to quote path parameters to correctly handle spaces:
#!/bin/bash
directory1=~/path/to/directory/
directory2=~/path/to/directory2/
diff -r "$directory1" "$directory2" || echo "files are different"