In bash, I would like to conditionally echo a new line depending on whether the current line in the terminal is empty or not.
Say, for example, first.sh runs first but I don't control it and I don't know what it will print each time. Can I get second.sh to start printing always on a brand new line and don't leave any blank lines above it?
first.sh
#!/bin/bash
let "n = 1"
max=$((RANDOM%3))
while [ "$n" -le "$max" ]
do
printf "%s" x
let "n += 1"
done
second.sh
#!/bin/bash
#if [ not_in_first_terminal_column ]
# echo
#fi
echo "Hola"
I want one of the following outputs
$ ./first.sh && ./second.sh
Hola
$ ./first.sh && ./second.sh
x
Hola
$ ./first.sh && ./second.sh
xx
Hola
but not
$ ./first.sh && ./second.sh
Hola
$ ./first.sh && ./second.sh
xHola
$ ./first.sh && ./second.sh
xxHola
Is it possible to do what I want? I figured using ANSI escape codes, something like in here, but I haven't found a way.