Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.debian.user > #265618 > unrolled thread

OT: coloured text?

Started byHans <hans.ullrich@loop.de>
First post2024-01-07 19:50 +0100
Last post2024-01-07 20:10 +0100
Articles 7 — 4 participants

Back to article view | Back to linux.debian.user


Contents

  OT: coloured text? Hans <hans.ullrich@loop.de> - 2024-01-07 19:50 +0100
    Re: OT: coloured text? Greg Wooledge <greg@wooledge.org> - 2024-01-07 20:10 +0100
      Re: OT: coloured text? Michael Kjörling <2695bd53d63c@ewoof.net> - 2024-01-07 20:20 +0100
        Re: OT: coloured text? Michael Kjörling <2695bd53d63c@ewoof.net> - 2024-01-07 20:30 +0100
          Re: OT: coloured text? Greg Wooledge <greg@wooledge.org> - 2024-01-07 21:00 +0100
        Re: OT: coloured text? Greg Wooledge <greg@wooledge.org> - 2024-01-07 20:30 +0100
    Re: OT: coloured text? Kent West <westk@acu.edu> - 2024-01-07 20:10 +0100

#265618 — OT: coloured text?

FromHans <hans.ullrich@loop.de>
Date2024-01-07 19:50 +0100
SubjectOT: coloured text?
Message-ID<HTGnD-1uZv-3@gated-at.bofh.it>
Hi folks, 

just a short question, please allow me to ask: 

How can I get coloured text output in a shell script? 

I am using:

echo "my text bla"

As far as I read and understood, this is not possible in every shell. I found 
nothing in bash manual for example.

Debian is using "dash" and not "bash", if I am correct, and with using the 
shebang line :!/bin/sh it is using dash, am I correct?

If I want coloured text, which shell do I have to use and what is the syntax 
then within the shell script?

I searched the web, but could not find a clear answer to this.

Thanks for a short answer.

Best regards

Hans    

[toc] | [next] | [standalone]


#265619

FromGreg Wooledge <greg@wooledge.org>
Date2024-01-07 20:10 +0100
Message-ID<HTGGZ-1vln-3@gated-at.bofh.it>
In reply to#265618
On Sun, Jan 07, 2024 at 07:40:28PM +0100, Hans wrote:
> How can I get coloured text output in a shell script? 

https://mywiki.wooledge.org/BashFAQ/037

> Debian is using "dash" and not "bash", if I am correct,

Debian installs BOTH of them by default.

Also by default, the /bin/sh symbolic link points to dash, rather
than to bash.  But this should not matter to you.

> shebang line :!/bin/sh it is using dash, am I correct?

You mean #!/bin/sh of course.  When selecting a shebang, you do so
based on which shell's syntax and features you are using in your
script.

If you're using bash syntax, then your shebang should be #!/bin/bash .

If you are NOT using any bash syntax or features, but strictly POSIX sh,
then you may use #!/bin/sh as your shebang.

The shebang tells the kernel which shell to execute to interpret your
script.  So, you match it up to whichever shell you're writing for.

> If I want coloured text, which shell do I have to use and what is the syntax 
> then within the shell script?

The wiki page has examples for both Bourne shell (which is a subset of
POSIX sh) and bash.

The approach I would take depends on how you're using colors in your
script.  Are you only doing them inside one function which writes an
error message and exits?  In this case, you might just use the tput
commands interleaved with printf or echo commands, as shown in the
first example.

If you're going to write *lots* of colored lines, you might choose to
store the escape sequences in global variables, and use them within
the function(s) that write colored messages.  Something like:

Program='MyProgram'
Red=$(tput setaf 1)
Normal=$(tput sgr0)
error() {
    printf 1>&2 '%s: %s%s%s\n' "$Program" "$Red" "$*" "$Normal"
}

All of this syntax is POSIX sh compatible, by the way.

[toc] | [prev] | [next] | [standalone]


#265621

FromMichael Kjörling <2695bd53d63c@ewoof.net>
Date2024-01-07 20:20 +0100
Message-ID<HTGQG-1voC-9@gated-at.bofh.it>
In reply to#265619
On 7 Jan 2024 14:03 -0500, from greg@wooledge.org (Greg Wooledge):
> The shebang tells the kernel which shell to execute to interpret your
> script.  So, you match it up to whichever shell you're writing for.

Or perhaps rather which interpreter to use to execute the file.
Examples of shebang lines which aren't for "shells" in the traditional
sense might be:

#!/usr/sbin/nft -f

#!/usr/bin/env python3

#!/usr/bin/perl

or if you are feeling evil... :-)

#!/bin/sed -e 1d

-- 
Michael Kjörling                     🔗 https://michael.kjorling.se
“Remember when, on the Internet, nobody cared that you were a dog?”

[toc] | [prev] | [next] | [standalone]


#265622

FromMichael Kjörling <2695bd53d63c@ewoof.net>
Date2024-01-07 20:30 +0100
Message-ID<HTH0l-1vrM-3@gated-at.bofh.it>
In reply to#265621
On 7 Jan 2024 14:20 -0500, from greg@wooledge.org (Greg Wooledge):
>> Examples of shebang lines which aren't for "shells" in the traditional
>> sense might be:
>> 
>> #!/usr/sbin/nft -f
>> 
>> #!/usr/bin/env python3
>> 
>> #!/usr/bin/perl
> 
> Python and perl are both scripting languages.  I don't know what nft is.

Yes, they are, but they are not typically used as interactive shells.
nft is nftables.


>> or if you are feeling evil... :-)
>> 
>> #!/bin/sed -e 1d
> 
> This is not a valid shebang.  You're only permitted ONE argument after
> the interpreter name.  You're going to execute /bin/sed '-e 1d' 'filename'
> with this.

Okay, fair point. In my defense, I tried it and it worked.

-- 
Michael Kjörling                     🔗 https://michael.kjorling.se
“Remember when, on the Internet, nobody cared that you were a dog?”

[toc] | [prev] | [next] | [standalone]


#265626

FromGreg Wooledge <greg@wooledge.org>
Date2024-01-07 21:00 +0100
Message-ID<HTHto-1vBb-13@gated-at.bofh.it>
In reply to#265622
On Sun, Jan 07, 2024 at 07:28:12PM +0000, Michael Kjörling wrote:
> On 7 Jan 2024 14:20 -0500, from greg@wooledge.org (Greg Wooledge):
> >> #!/bin/sed -e 1d
> > 
> > This is not a valid shebang.  You're only permitted ONE argument after
> > the interpreter name.  You're going to execute /bin/sed '-e 1d' 'filename'
> > with this.
> 
> Okay, fair point. In my defense, I tried it and it worked.

unicorn:~$ seq 5 | sed '-e 1d'
2
3
4
5

Hmm... my first thought was that GNU sed is being lenient here, but on
further reflection I'm not sure about that.  This is equivalent to
sed -e ' 1d' (with a leading space in front of the script which is the
option-argument of -e).  And perhaps sed permits leading whitespace in
each line of its scripts, for purposes of readability.

In any case, trying to put multiple arguments on the shebang won't work
in general.  It's a well-known behavior, and is why GNU env(1) has -S.

[toc] | [prev] | [next] | [standalone]


#265623

FromGreg Wooledge <greg@wooledge.org>
Date2024-01-07 20:30 +0100
Message-ID<HTH0l-1vrM-5@gated-at.bofh.it>
In reply to#265621
On Sun, Jan 07, 2024 at 07:12:38PM +0000, Michael Kjörling wrote:
> On 7 Jan 2024 14:03 -0500, from greg@wooledge.org (Greg Wooledge):
> > The shebang tells the kernel which shell to execute to interpret your
> > script.  So, you match it up to whichever shell you're writing for.
> 
> Or perhaps rather which interpreter to use to execute the file.

"Interpreter" is the traditional word for the program executed by the
kernel, yes.

> Examples of shebang lines which aren't for "shells" in the traditional
> sense might be:
> 
> #!/usr/sbin/nft -f
> 
> #!/usr/bin/env python3
> 
> #!/usr/bin/perl

Python and perl are both scripting languages.  I don't know what nft is.

> or if you are feeling evil... :-)
> 
> #!/bin/sed -e 1d

This is not a valid shebang.  You're only permitted ONE argument after
the interpreter name.  You're going to execute /bin/sed '-e 1d' 'filename'
with this.

[toc] | [prev] | [next] | [standalone]


#265620

FromKent West <westk@acu.edu>
Date2024-01-07 20:10 +0100
Message-ID<HTGGZ-1vln-7@gated-at.bofh.it>
In reply to#265618

[Multipart message — attachments visible in raw view] — view raw

On Sun, Jan 7, 2024 at 12:41 PM Hans <hans.ullrich@loop.de> wrote:

> Hi folks,
>
> just a short question, please allow me to ask:
>
> How can I get coloured text output in a shell script?
>
> I am using:
>
> echo "my text bla"
>
> As far as I read and understood, this is not possible in every shell. I
> found
> nothing in bash manual for example.
>
> Debian is using "dash" and not "bash", if I am correct, and with using the
> shebang line :!/bin/sh it is using dash, am I correct?
>
> If I want coloured text, which shell do I have to use and what is the
> syntax
> then within the shell script?
>
> I searched the web, but could not find a clear answer to this.
>
> Thanks for a short answer.
>
> Best regards
>
> Hans
>
>
>
"sh" is probably "/usr/bin/sh" ("$ command -v sh" or the (now-deprecated?)
"which sh" will show you which "sh" is being called), which is probably a
symlink to "dash" ("ls -lah <path>" will show that to you).

But not necessarily.

Perhaps one of the easiest ways to get color in a bash/dash/sh shell is to
use ANSI Terminal Escape Sequences. Not 100% portable, but probably
suitable for what you want.

Something like:

#!/bin/env sh

printf "\033[31m"
printf "Now we're printing in Red\n"
printf "\033[0m"
printf "Now we're printing in the system default color.\n"



-- 
Kent West                    <")))><
IT Support / Client Support
Abilene Christian University
Westing Peacefully - http://kentwest.blogspot.com

[toc] | [prev] | [standalone]


Back to top | Article view | linux.debian.user


csiph-web