如何将一个字符串分割成由bash shell中至少有一个空格分隔多个字符串? [英] How to split one string into multiple strings separated by at least one space in bash shell?

查看:1458
本文介绍了如何将一个字符串分割成由bash shell中至少有一个空格分隔多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串,就像一个句子,我知道肯定有很多的话至少有一个空间,每相邻的两个独立的。我怎么能分裂成单词串个人这样我就可以通过他们的循环?

I have a string, like a sentence, and I know for sure that there are many words with at least one space separate every adjacent two. How can I split the words into individual strings so I can loop through them?


编辑:
巴什
该字符串传递作为参数。例如$ {2}可能是猫,猫,文件。所以哪能通过这个ARG $循环{2}?

Bash The string is passed in as an argument. e.g. ${2} might be "cat '' cat '' file". so how can I loop through this arg ${2}?


另外,如何检查是否字符串包含空格?

Also, how to check if a string contains spaces?

推荐答案

你尝试只是路过字符串变量为循环?击为一体,将自动分割的空白。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆