Often it has been said that echo is neither portable nor correct.
Here is an input.txt:
line1
line2
-n
line4
-en
line6
-x
line8
Then the following fails with BSD/Linux/bash:
while IFS= read line
do
echo "$line"
done < input.txt
It is elegantly improved by means of an echo function:
echo_(){
( IFS=" "; printf "%s\n" "$*" )
}
while IFS= read line
do
echo_ "$line"
done < input.txt
(Of course you can directly use the printf instead of the echo function.)
Further it provides compatibility with ksh/sh/psh/ash/dash, and even an old Bourne shell might work.
The following is a suite of echo functions that lets you easily go into a script and replace
-
echo
withecho_
-
echo -n
withecho_n
-
echo -e
withecho_e
-
echo -ne
orecho -n -e
withecho_ne
-
echo -en
orecho -e -n
withecho_ne
(orecho_en
)
# Simple echo
echo_(){
( IFS=" "; printf "%s\n" "$*" )
}
# Portable echo -n
echo_n() {
( IFS=" "; printf "%s" "$*" )
}
# Portable echo -e
echo_e() {
( IFS=" "; printf "%b\n" "$*" )
}
# Portable echo -ne
echo_ne() {
( IFS=" "; printf "%b" "$*" )
}
alias echo_en=echo_ne