Thursday, January 20, 2011

Using 'trim' PHP function in bash

Today during writing some scripts I had a need of using PHP function 'trim' in bash script.
This code gave me perfect results:

#!/bin/sh

trim() { echo $1; }

echo ">>$(trim 'right side     ')<<"
echo ">>$(trim '     left side')<<"
echo ">>$(trim '     both sides     ')<<"


Of course, second part (with echo) was written to test different cases whether it works correctly. Solution found here.