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.

1
  • 3
    Spaces matter! There should not be one after =.
    – muru
    Commented Jan 5, 2015 at 16:40

1 Answer 1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .