Manolo Dominguez Becerra
Manolo Dominguez Becerra

Reputation: 1373

Reading variables from config file results in "VARNAME: command not found"

What is wrong with these lines?

FASTAQ1 = "/home/mdb1c20/my_onw_NGS_pipeline/files/fastq/W2115220_S5_L001_R1_001.fastq"
FASTAQ2 = "/home/mdb1c20/my_onw_NGS_pipeline/files/fastq/W2115220_S5_L001_R2_001.fastq"
DIRECTORY = "/home/mdb1c20/my_onw_NGS_pipeline/scripts_my_first_NGS"

They are in a .conf file with other similar variables. The only difference is that these three are created with printf

printf 'FASTAQ1 = "%s"\n' "$FASTA1" >> "$DIRECTORY/$filename1/scripts/shortcut.config"
printf 'FASTAQ2 = "%s"\n' "$FASTA2" >> "$DIRECTORY/$filename1/scripts/shortcut.config"
printf 'DIRECTORY = "%s"\n' "$DIRECTORY" >> "$DIRECTORY/$filename1/scripts/shortcut.config"

When a script I am using open the .confi file its says that FASTAQ1: command not found

Apart from these three, the rest of variables were created manually in a archive .conf file but the script add these three on the go. The only thing I haven't tried because I don't know how to do that is to remove the white spaces before and after the equal simbol?

Upvotes: 0

Views: 218

Answers (2)

konsolebox
konsolebox

Reputation: 75568

If you intended to source your configuration file, you should have used printf this way:

printf 'FASTAQ1=%q\n' "$FASTA1" >> "$DIRECTORY/$filename1/scripts/shortcut.config"

This allows you to store the value safely regardless if it has spaces or quotes.

The error was caused by the assignment command being interpretted as a simple command instead because of the spaces around the equal sign.

Alternatively for Bash 4.4+, you can use @Q expansion:

echo "FASTAQ1=${FASTA1@Q}" >> "$DIRECTORY/$filename1/scripts/shortcut.config"

Upvotes: 1

MattArmstrong
MattArmstrong

Reputation: 384

In bash, this:

var = value

is not the same as this:

var=value

The first example runs a command named "var" and passes it two arguments "=" and "value".

The second example sets a variable called "var" to "value".

It was hard to find this detail in the Bash manual, but the difference is between simple commands and assigning to variables, or shell parameters.

Upvotes: 1

Related Questions