Question

I've a little dilemma right now and I'm writing a small shell script that creates 10 files, and then within each file its file name is written.

This is what I have so far:

for f in {0..9}.txt
do
    echo Hello, this is the first line of '#$f' > "File${f}"

done

So output for each file should be:

Hello, this is the first line of file0 

Hello, this is the first line of file1

etc.

Était-ce utile?

La solution

When you use single quotes, the shell will not expand what is inside them. So '#$f' is treated as literally the string #$f instead of being expanded to the value of $f. Try this instead:

for f in {0..9}.txt; do
    echo "Hello, this is the first line of file #$f" > "File${f}"
done

Example:

$ cat File3.txt
Hello, this is the first line of file #3.txt
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top