Your problem is because you entered a command line that contained an unmatched single-quote character. It has nothing to do with echo
. The shell is issuing a secondary prompt to let you know that it is expecting the end of the single-quoted string started by 's po...
You'd need to escape that '
if you want it passed litterally to the log
function:
log "this is a testing's post"
Or:
log this is a testing\'s post
for instance.
Now, where you'd need to escape characters from echo -e
is for the backslash characters. As for instance if you call it as log '\begin'
, that \b
would be translated to a BS character by echo -e
.
To address that, either store the escape sequences with those \e
expanded in those variables:
log() {
RED=$'\e[0;31m'
RESET=$'\e[0m'
printf '%s\n' "${RED}$(date) ${RESET}$*" >> "$HOME"/mylog.txt
}
(here using bash
, ksh93
, mksh
, zsh
or FreeBSD sh
syntax for that not-yet-POSIX $'...'
syntax).
Or use this syntax:
log() {
RED='\e[0;31m'
RESET='\e[0m'
printf '%b%s%b %s\n' "$RED" "$(date)" "$RESET" "$*" >> "$HOME"/mylog.txt
}
Note that the expansion of "$*"
depends on the current value of $IFS
.
In any case, it's better to avoid echo
for arbitrary data.
tput setaf
. See the following page: http://mywiki.wooledge.org/BashFAQ/037 – Valentin Bajrami Nov 30 '15 at 14:18