Possible Duplicate:
Printf example in bash does not create a newline
I have a sample script “array-test.sh” which aggregates three functions into one array:
[user@host ~]$ cat array-test.sh
#!/usr/bin/env bash
function1() {
printf '%s\n\n\n' "cat"
}
function2() {
printf '%s\n\n\n' "dog"
}
function3() {
printf '%s\n\n\n' "mouse"
}
for function in\
function1\
function2\
function3; do
array[$((index++))]=$($function)
done
echo "${array[@]}"
[user@host ~]$ ./array-test.sh
cat dog mouse
[user@host ~]$
However, newline characters are missing. What causes such behavior?
As indicated by ÁlvaroG.Vicario, backticks (and
$()) remove trailing newlines. There’s no escaping it, so if you must, you’ll have to work around it: