The following script prints a newline before the first word printed by echo. Why?
Environment:
- macOS Mojave
- using either system sh (which is GNU bash 3.2.57(1), or GNU bash 5.1.8(1)
Description
Here's the script, which I tried with different shebang lines (bash or sh) ending with the same problem:
#!/bin/sh
which sbcl > /dev/null
status=$?
if [ $status -eq 0 ]; then
export PGMFOR_LISP=`which sbcl`
echo -1-
echo "Using Common Lisp - " `sbcl --version` "- from:" $PGMFOR_LISP
echo -2-
else
echo "**Error!** Request to use SBCL, but fail to find it in PATH!"
fi
It prints the following:
-1-
Using Common Lisp - SBCL 2.1.4 - from: /usr/local/bin/sbcl
-2-
The issue I have is the empty line between the -1-
line and the Using Common Lisp ...
line.
The above uses sbcl --version
to get the version. It prints on stdout and does print a newline before the string it outputs. I assume the problem comes from that, but I have not been able to fix it with tr -d
for example.
Here's the following extra tests:
>Pierres-iMac@Fri May 14@07:25:39[~]
> sbcl --version 2>/dev/null | od -tc
0000000 S B C L 2 . 1 . 4 \n
0000013
>Pierres-iMac@Fri May 14@07:26:06[~]
> sbcl --version 2>/dev/null | hexdump
0000000 53 42 43 4c 20 32 2e 31 2e 34 0a
000000b
>Pierres-iMac@Fri May 14@07:30:19[~]
>
How can I get rid of that extra empty line?
echo go; sbcl --version >/dev/null; echo done
do you get a blank line betweengo
anddone
?$PGMFOR_LISP
for the user, or just while your script runs?>/dev/null
by2>/dev/null
no empty line show between go and done. However if I useecho "Using Common Lisp - " `sbcl --version 2>/dev/null` "- from:" $PGMFOR_LISP
it still outputs an empty line.