strtolower()

# strtolower() -- — Make a string lowercase
# Usage: strtolower string
#
# Example 1:
#
# echo $(strtolower "SAYA SUKAN MAKAN");
#
# Example 2:
#
# string="$(strtolower "SAYA SUKAN MAKAN")";
# echo $string;

if [ -n "${BASH_VERSINFO[0]}" ] && [ ${BASH_VERSINFO[0]} -gt 3 ]; then
	# Updated: 20-Aug-2009 - for bash version 4
	strtolower() {
		[ $# -eq 1 ] || return 1;
		echo ${1,,};
		return 0;
	}
else
	strtolower() {
		[ $# -eq 1 ] || return 1;
		local _str _cu _cl _x;
		_cu=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
		_cl=(a b c d e f g h i j k l m n o p q r s t u v w x y z);
		_str=$1;
		for ((_x=0;_x<${#_cl[*]};_x++)); do
			_str=${_str//${_cu[$_x]}/${_cl[$_x]}};
		done;
		echo $_str;
		return 0;
	}
fi

Leave a Reply

Your email address will not be published. Required fields are marked *