请从bash的变量空白 [英] remove whitespace from bash variable

查看:101
本文介绍了请从bash的变量空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设变量包含空格,换行,并遵循一些文本标签,为什么这样的:

Assuming a variable contains spaces, newlines, and tabs followed by some text, why does this:

${var#"${var%%[![:space:]]*}"}  # strip var of everything 
                                # but whitespace
                                # then remove what's left 
                                # (i.e. the whitespace) from var

删除空白和离开文本,但这样的:

remove the white space and leave the text, but this:

${var##[:space:]*}  # strip all whitespace from var

不?

推荐答案

如果我设置 VAR =这是一个测试,无论你的建议不工作;
只是领先的东西被删除。
为什么不使用替换功能,消除空白的所有实例,而不仅仅是第一个:

If I set var=" This is a test ", both your suggestions do not work; just the leading stuff is removed. Why not use the replace functionality that removes all occurrences of whitespace and not just the first:

 ${var//[[:space:]]}

这篇关于请从bash的变量空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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