如何在bash shell中将一个字符串拆分为由至少一个空格分隔的多个字符串? [英] How to split one string into multiple strings separated by at least one space in bash shell?
本文介绍了如何在bash shell中将一个字符串拆分为由至少一个空格分隔的多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含许多单词的字符串,每两个单词之间至少有一个空格.如何将字符串拆分为单个单词以便循环遍历它们?
I have a string containing many words with at least one space between each two. How can I split the string into individual words so I can loop through them?
字符串作为参数传递.例如.${2} == "cat cat 文件"
.我怎样才能遍历它?
The string is passed as an argument. E.g. ${2} == "cat cat file"
. How can I loop through it?
另外,如何检查字符串是否包含空格?
Also, how can I check if a string contains spaces?
推荐答案
您是否尝试将字符串变量传递给 for
循环?例如,Bash 会自动在空白处拆分.
Did you try just passing the string variable to a for
loop? Bash, for one, will split on whitespace automatically.
sentence="This is a sentence."
for word in $sentence
do
echo $word
done
This
is
a
sentence.
这篇关于如何在bash shell中将一个字符串拆分为由至少一个空格分隔的多个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文