Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a script that gets the filenames in a folder and creates HTML link to them on my homepage. Here is the script:

#!/bin/bash  
list_dir=`ls -t /path/to/dir/`
for i in $list_dir   
do  
echo `'<a href="/path/to/dir/$i">$i</a>' >> /var/www/index.html`   
done

But when I check the var/www/index.html the code is submitted, but the variables have not been substituted. Any advice on how to fix this?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

There is a reason both single quotes ' and double quotes " exist. Parameters get expanded within double quotes but not within single quotes.

echo "<a href=\"/path/to/dir/$i\">$i</a>" >> /var/www/index.html
share|improve this answer

The problem is that you have single quotes wrapping the variables. You're variables are considered literals when wrapped by singled quotes. Try switching them to double quotes instead.

You also need to remove the back ticks around the "echo ...". Those exec a sub-shell which gives you this error:

a: line 5: <a href="/path/to/dir/file1">file1</a>: No such file or directory

a: line 5: <a href="/path/to/dir/file2">file2</a>: No such file or directory

This script does what you need:

#!/bin/bash
list_dir=`ls -t /path/to/dir/`
for i in $list_dir
do
echo "<a href=\"/path/to/dir/$i\">$i</a>" >> /var/www/index.html
done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.