Glenn AWB
Glenn AWB

Reputation: 3

echo $PATH literally prints "$PATH" as the output

I want to go through a text file, that contains one command per line. I want to read each line, execute each command, and then save the output to a file.

The part of the script that's giving me problems is:

echo COMMAND LOG > $dest/command.log

echo ====== >> $dest/command.log

while IFS= read -r v

do
    echo ====== >> $dest/command.log

    echo COMMAND: $v >> $dest/command.log

    echo ======OUTPUT====== >> $dest/command.log

    $v >> $dest/command.log

    echo ====== >> $dest/command.log

done < "$commands"

It works great for everything except "echo $PATH" which generates:

======

COMMAND: echo $PATH

======OUTPUT======

$PATH

Is there anyway to get it to work properly? Thanks in advance.

Upvotes: 0

Views: 930

Answers (1)

Arount
Arount

Reputation: 10403

It's expected, because you are fetching a file line by line, you got strings, not variables. $v is a "$PATH" string, that's all.

You must eval your string to get associated variable's value:

echo COMMAND: $(eval $v) >> $dest/command.log

Upvotes: 1

Related Questions