假设有以下三个句子:
sentence_one="This is the first sentence." sentence_two="This is the second sentence." sentence_three="This is the third sentence."
可以使用Bash中的字符串连接符(+)来将它们转换为一行:
combined_sentence=$sentence_one" "$sentence_two" "$sentence_three
另一种方法是使用echo和命令替换的组合,并将它们重定向到文件中:
echo $sentence_one $sentence_two $sentence_three > combined_sentence.txt
无论哪种方法,结果都将是:
This is the first sentence. This is the second sentence. This is the third sentence.
代码示例1:
#!/bin/bash
sentence_one="This is the first sentence." sentence_two="This is the second sentence." sentence_three="This is the third sentence."
combined_sentence=$sentence_one" "$sentence_two" "$sentence_three
echo $combined_sentence
代码示例2:
#!/bin/bash
sentence_one="This is the first sentence." sentence_two="This is the second sentence." sentence_three="This is the third sentence."
echo $sentence_one $sentence_two $sentence_three > combined_sentence.txt