You've got the quoting wrong.
If you want to simulate the behaviour of echo
, your function should accept multiple parameters, and print them all. Currently it's only evaluating the first parameter, so I suggest using $*
instead. You also need to enclose the argument in double quotes to protect any special characters:
echo_message() {
echo -e "${lbGREEN}$*${NC}"
}
The special variable $*
expands to all the arguments, separated by spaces (or more accurately, the first character of $IFS, which is usually a space character). Note that you almost always want "$@"
instead of "$*"
, and this is one of the rare occasions where the latter is also correct, though with slightly different semantics if IFS
is set to a non-standard value.
Now the function supports multiple arguments, and prints them all in green, separated by spaces. However, I would recommend that you also quote the argument when calling the function:
echo_message "$normalMessage"
While spaces in $normalMessage
will now be treated correctly, other special characters like !
will still require the quotes.