bash 如何从字符串中删除空格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13659318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:02:27  来源:igfitidea点击:

How to remove space from string?

bashshell

提问by Hitul Mistry

In ubuntu bash script how to remove space from one variable

在 ubuntu bash 脚本中如何从一个变量中删除空格

string will be

字符串将是

   3918912k 

Want to remove all blank space.

想要删除所有空格。

回答by squiguy

The tools sedor trwill do this for you by swapping the whitespace for nothing

工具sedtr将通过交换空白为您完成此操作

sed 's/ //g'

sed 's/ //g'

tr -d ' '

tr -d ' '

Example:

例子:

$ echo "   3918912k " | sed 's/ //g'
3918912k

回答by Gilles Quenot

Try doing this in a shell:

尝试在 shell 中执行此操作:

s="  3918912k"
echo ${s//[[:blank:]]/}

That uses parameter expansion(it's a non posixfeature)

使用参数扩展(这是一个非posix功能)

[[:blank:]]is a POSIX regex class (remove spaces, tabs...), see http://www.regular-expressions.info/posixbrackets.html

[[:blank:]]是 POSIX 正则表达式类(删除空格、制表符...),请参阅http://www.regular-expressions.info/posixbrackets.html

回答by Micka?l Le Baillif

You can also use echoto remove blank spaces, either at the beginning or at the end of the string, but also repeating spaces inside the string.

您还可以使用echo删除字符串开头或结尾的空格,以及字符串内的重复空格。

$ myVar="    kokor    iiij     ook      "
$ echo "$myVar"
    kokor    iiij     ook      
$ myVar=`echo $myVar`
$
$ # myVar is not set to "kokor iiij ook"
$ echo "$myVar"
kokor iiij ook

回答by gniourf_gniourf

A funny way to remove all spaces from a variable is to use printf:

从变量中删除所有空格的有趣方法是使用 printf:

$ myvar='a cool variable    with   lots of   spaces in it'
$ printf -v myvar '%s' $myvar
$ echo "$myvar"
acoolvariablewithlotsofspacesinit

It turns out it's slightly more efficient than myvar="${myvar// /}", but not safe regarding globs (*) that can appear in the string. So don't use it in production code.

事实证明myvar="${myvar// /}",对于*可以出现在字符串中的globs ( ) 来说,它比 更有效,但并不安全。所以不要在生产代码中使用它。

If you really really want to use this method and are really worried about the globbing thing (and you really should), you can use set -f(which disables globbing altogether):

如果你真的很想使用这个方法并且真的很担心通配符(你真的应该这样做),你可以使用set -f(它完全禁用通配符):

$ ls
file1  file2
$ myvar='  a cool variable with spaces  and  oh! no! there is  a  glob  *  in it'
$ echo "$myvar"
  a cool variable with spaces  and  oh! no! there is  a  glob  *  in it
$ printf '%s' $myvar ; echo
acoolvariablewithspacesandoh!no!thereisaglobfile1file2init
$ # See the trouble? Let's fix it with set -f:
$ set -f
$ printf '%s' $myvar ; echo
acoolvariablewithspacesandoh!no!thereisaglob*init
$ # Since we like globbing, we unset the f option:
$ set +f

I posted this answer just because it's funny, not to use it in practice.

我发布这个答案只是因为它很有趣,而不是在实践中使用它。

回答by Tim Pote

Since you're using bash, the fastest way would be:

由于您使用的是 bash,因此最快的方法是:

shopt -s extglob # Allow extended globbing
var=" lakdjsf   lkadsjf "
echo "${var//+([[:space:]])/}"

It's fastest because it uses built-in functions instead of firing up extra processes.

它是最快的,因为它使用内置函数而不是启动额外的进程。

However, if you want to do it in a POSIX-compliant way, use sed:

但是,如果您想以符合 POSIX 的方式执行此操作,请使用sed

var=" lakdjsf   lkadsjf "
echo "$var" | sed 's/[[:space:]]//g'