DEV Community

Eduardo Issao Ito
Eduardo Issao Ito

Posted on

2 1

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay