DEV Community 👩‍💻👨‍💻

DEV Community 👩‍💻👨‍💻 is a community of 967,611 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Eduardo Issao Ito
Eduardo Issao Ito

Posted on

Trim a string in shell script

Nice trick just using echo command:

$ str="  a string  "
$ echo "[$str]"
[  a string  ]

$ new=$(echo $str)
$ echo "[$new]"
[a string]

To trim an entire file:

sed 's/^[ \t]*//;s/[ \t]*$//' < input_file

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.