简体   繁体   English

在同一行执行 2 个 bash 命令

[英]Execute 2 bash commands on same line

I made a bash script to output the content for the *txt files and to print also the name of the file executed on the same line.我制作了一个 bash 脚本来输出*txt文件的内容并打印在同一行上执行的文件的名称。 But for no reason the first line has only the output and the next line has the name of the first output + the output of the next file.但无缘无故第一行只有输出,下一行有第一个输出的名称+下一个文件的输出。 How can I have the file name + output ?我怎么能有file name + output

for i in *.txt; do cat "$i" && echo -n "$i"; done

Outputs:输出:

ignore
alex.txt11111
alex1.txtda
alex2.txtnu
alex3.txt

Correct would be正确的是

alex.txt ignore; alex1.txt 11111; alex2.txt da; alex3.txt nu

You are printing the contents of the file first, then printing its name without a newline ( echo -n ).您首先打印文件的内容,然后在没有换行符的情况下打印其名称( echo -n )。 You should do the opposite: print the name without a newline, but with an extra space at the end, then print the contents of the file.你应该做相反的事情:打印没有换行符的名称,但在末尾有一个额外的空格,然后打印文件的内容。

for f in *.txt; do
    echo -n "$f "
    cat "$f"
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM