Languages
[Edit]
EN

Bash - trim string

3 points
Created by:
Mark-Rotteveel
537

In this article, we would like to show you how to trim string using Bash.

Quick solution:

Note:

On the internet you can also find a solution using xargs, however it was not created to trim text and may damage your code.

 

Practical example

Edit

In this example, we create a reusable function that uses awk to trim the string that we pass as the first argument ($1).

Alternative solution

Edit

In this example, we present a script that uses Bash wildcards to trim whitespace characters from the variable string.

script.sh file:

Run in command line:

Output:

 

Alternative titles

  1. Bash - trim string variable
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join