1

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?

10
  • 1
    If you run echo go; sbcl --version >/dev/null; echo done do you get a blank line between go and done? Commented May 13, 2021 at 22:18
  • Are you trying to set $PGMFOR_LISP for the user, or just while your script runs? Commented May 13, 2021 at 22:19
  • @FrankThomas: No change occurs if I quote and replace the line with: echo "-1-", it still issues a newline.
    – PRouleau
    Commented May 13, 2021 at 22:22
  • @roima, I'm doing both: I am using it inside the script and also use it later, after the script has run.
    – PRouleau
    Commented May 13, 2021 at 22:23
  • 1
    sbcl seems to output to the standard error stream. If I replace the >/dev/null by 2>/dev/null no empty line show between go and done. However if I use echo "Using Common Lisp - " `sbcl --version 2>/dev/null` "- from:" $PGMFOR_LISP it still outputs an empty line.
    – PRouleau
    Commented May 13, 2021 at 22:32

1 Answer 1

0
# source me
if which sbcl >/dev/null
then
    export PGMFOR_LISP=`which sbcl`
    printf "%s\n" '-1-'
    printf "Using Common Lisp - %s - from %s\n" "$(sbcl --version 2>/dev/null)" "$PGMFOR_LISP"
    printf "%s\n" '-2-'
else
    echo '**Error!** Request to use SBCL, but fail to find it in PATH!'
fi

There's little point having an initial #! line because this script can only usefully be sourced (possibly with ., again depending on the shell).

I've replaced the echo -1- type lines because using echo with an argument that starts with a dash is not well defined - different shells will do different things.


For the purposes of testing the apparently strange behaviour of sbcl --version, I've used this mockup test script, executable and in my $PATH:

#!/bin/bash
echo >&2
echo "SBCL 2.1.4"

I cannot reproduce the extra blank line you're reporting

export PGMFOR_LISP=`which sbcl`
printf "Using Common Lisp - %s - from %s\n" "$(sbcl --version 2>/dev/null)" "$PGMFOR_LISP"

results in just the single line

Using Common Lisp - SBCL 2.1.4 - from /home/roaima/bin/sbcl
3
  • @roima I tried it verbatim and it's still printing the empty line between -1- and Using... I assume it works in your environment?
    – PRouleau
    Commented May 13, 2021 at 22:47
  • BTW, the echo "-1-" and "-2" are only in there to show the empty line. I'd normally would not have these lines. But thanks for pointing out the potential portability issue with it.
    – PRouleau
    Commented May 13, 2021 at 22:51
  • I just saw a SO tag for SBCL, I check if there is info there.
    – PRouleau
    Commented May 14, 2021 at 12:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .