How to remove spaces or specified characters in a string in Shell

How to remove spaces or specified characters in a string in Shell

There are many methods on the Internet that, although correct, do not produce correct results when used. Here is the correct method.

Remove leading spaces

$text=" 123 456 "
# This way of writing ensures the correct result.
text=`echo $text | sed -e 's/^[ \t]*//g'`
# These methods are not tested, please refer to the above for writing.
# Remove trailing spaces sed 's/[ \t]*$//g'
# Delete leading and trailing spaces, but not spaces in between sed -e 's/^[ \t]*//g' -e 's/[ \t]*$//g'
# Remove all spaces in a string sed 's/[[:space:]]//g'

Of course there is an easy way:

# Replace only one text=${text/ /-}
# Replace all text=${text// /-}

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. Thank you for your support of 123WORDPRESS.COM. If you want to learn more about this, please check out the following links

You may also be interested in:
  • Shell script reads content from a file line by line in several ways
  • Several examples of using pipelines in Shell scripts
  • Shell script uses for loop to traverse parameters
  • Usage of awk command in Shell script
  • Several methods of string sorting in Shell
  • Several ways to calculate integers in Shell
  • One command lets you understand the common parameters of the read command in the shell
  • Several methods to count the number of words in a string in Shell
  • How to remove leading and trailing spaces in a string in Shell
  • Using getopts in Shell Scripts to Process Multiple Command Line Options

<<:  MySQL slow query log configuration and usage tutorial

>>:  In-depth understanding of the implementation principle of require loader

Recommend

js to realize the rotation of web page pictures

This article shares the specific code of js to re...

MySQL limit performance analysis and optimization

1. Conclusion Syntax: limit offset, rows Conclusi...

MySQL 5.5.56 installation-free version configuration method

The configuration method of MySQL 5.5.56 free ins...

How to use boost.python to call c++ dynamic library in linux

Preface Recently I started using robot framework ...

Summary of solutions to common Linux problems

1. Connect Centos7 under VMware and set a fixed I...

Detailed explanation of CSS animation attribute keyframes

How long has it been since I updated my column? H...

HTML is the central foundation for the development of WEB standards

HTML-centric front-end development is almost what ...

Detailed explanation of CSS multiple three-column adaptive layout implementation

Preface In order to follow the conventional WEB l...

CSS implements a pop-up window effect with a mask layer that can be closed

Pop-up windows are often used in actual developme...

Docker deploys net5 program to achieve cross-platform functions

Deployment environment: docker container, liunx s...

An example of how to quickly deploy web applications using Tomcat in Docker

After learning the basic operations of Docker, we...