0

I can't realize why if I just try to concat some file paths (without opening the files), I get "permission denied":

#!/bin/bash

if [ "$#" -eq 1 ]; then
        path1= "./src/$1"
        path2= "./Main/$1"
        echo $path1
else
        echo "Invalid number of parameters"
fi

This is how I execute the bash script (named compare.sh):

enter image description here

PS: ./src/GraphicTest.cpp and ./Main/GraphicTest.cpp are existing files.

  • 3
    Spaces matter! There should not be one after =. – muru Jan 5 '15 at 16:40
2

Shell variables are set using the syntax name=value. There must not be any [unquoted] whitespace characters in the declaration.

    path1="./src/$1"
    path2="./Main/$1"
    #     ^ remove space here

The space in your usage was invoking the other usage of name=value, i.e. temporarily setting environment variables for a single program invocation.

For instance:

# set environment variable "foo" to "bar" for this invocation of "program"
foo="bar" /path/to/program
# set "baz" to an empty value for the invocation of "program"
baz= /path/to/program

The reason you were getting a permission denied error on path1= "./src/$1" was because it was taken to mean "Set environment variable path1 to empty and invoke program ./src/$1" and since ./src/$1 doesn't have its executable bit set it failed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.