Daven Patel
Daven Patel

Reputation: 176

Bash Scripting Export Filename to Variable

I'm trying to set a variable to a string representing a file's location and when I try to set the variable I keep on getting a 'permission denied' error, because the bash script is trying to execute the file.

Here is the code I'm using

date= 07062011
archive_dir=~/Documents/ABC/Testing
aggregate_file= `echo ${archive_dir}"/"${date}"_Aggregated.txt"`

The error I am getting is the following:

./8000.2146701.sh: line 32: /home/me/Documents/ABC/Testing/20110706_Aggregated.txt: Permission denied

From what I understand using the backticks should allow me to take the output of the command and put it into a variable and it doesn't seem to be working. When I just do the echo statement by itself the output is the file path.

Thanks.

Upvotes: 4

Views: 9226

Answers (2)

C. Ramseyer
C. Ramseyer

Reputation: 2382

Don't use backticks and echo, just aggregate_file="${archive_dir}/${date}_Aggregated.txt". And no spaces, as another poster mentioned.

Upvotes: 2

DigitalRoss
DigitalRoss

Reputation: 146053

The direct problem is the space following:

aggregate_file=

That is:

aggregate_file= `echo ${archive_dir}"/"${date}"_Aggregated.txt"`

would have worked by simply removing the space after the assignment operator:

aggregate_file=`echo ${archive_dir}"/"${date}"_Aggregated.txt"`

But really it should have just been:

aggregate_file="${archive_dir}/${date}_Aggregated.txt"

Upvotes: 9

Related Questions