DEV Community

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

Oldest comments (0)