我现在有点进退两难,我正在编写一个小 shell 脚本来创建 10 个文件,然后在每个文件中写入其文件名。
这是我到目前为止所拥有的:
for f in {0..9}.txt
do
echo Hello, this is the first line of '#$f' > "File${f}"
done
因此每个文件的输出应该是:
Hello, this is the first line of file0
Hello, this is the first line of file1
等等
最佳答案
当您使用单引号时,shell 不会扩展其中的内容。因此,'#$f'
被视为字面上的字符串 #$f
,而不是扩展为 $f
的值。试试这个:
for f in {0..9}.txt; do
echo "Hello, this is the first line of file #$f" > "File${f}"
done
示例:
$ cat File3.txt
Hello, this is the first line of file #3.txt
关于linux - 将文件的文件名写入 Linux/fedora 自身。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22529391/