Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash Scripting Export Filename to Variable

Tags:

linux

bash

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.

like image 710
Daven Patel Avatar asked Sep 16 '25 11:09

Daven Patel


1 Answers

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"
like image 152
DigitalRoss Avatar answered Sep 18 '25 09:09

DigitalRoss