Make script output text as default color scheme of terminal [UNSOLVABLE]

Forum rules
Before you post please read how to get help
Post Reply
User avatar
linx255
Level 5
Level 5
Posts: 663
Joined: Mon Mar 17, 2014 12:43 am

Make script output text as default color scheme of terminal [UNSOLVABLE]

Post by linx255 » Tue Apr 24, 2018 2:55 am

Hi,

I noticed that when I do tree -R in mate-terminal the directories and files listed are color-coded which really helps readability. However, when running this command in a script it's all one color. How can I make the script display text per the color scheme of mate-terminal?

Thanks
Last edited by linx255 on Wed Apr 25, 2018 10:43 pm, edited 1 time in total.
- I'm running Mint 18 Mate 64-bit
- 4.4.0-21-generic x86_64
- All my bash scripts begin with #!/bin/bash

User avatar
catweazel
Level 16
Level 16
Posts: 6230
Joined: Fri Oct 12, 2012 9:44 pm
Location: Australian Antarctic Territory

Re: Make script output text as default color scheme of terminal

Post by catweazel » Tue Apr 24, 2018 3:08 am

linx255 wrote:
Tue Apr 24, 2018 2:55 am
I noticed that when I do tree -R in mate-terminal the directories and files listed are color-coded which really helps readability. However, when running this command in a script it's all one color. How can I make the script display text per the color scheme of mate-terminal?
man tree

Code: Select all

       -C     Turn  colorization  on  always, using built-in color defaults if
              the LS_COLORS environment variable is not set.  Useful  to  col‐
              orize output to a pipe.

Code: Select all

#!/bin/bash
tree -R -C
Only a life lived for others is a life worthwhile - Albert Einstein

User avatar
linx255
Level 5
Level 5
Posts: 663
Joined: Mon Mar 17, 2014 12:43 am

Re: Make script output text as default color scheme of terminal

Post by linx255 » Tue Apr 24, 2018 2:17 pm

Oh I didn't expect that to be embedded in any one command. Is there a way to just make a script use the default terminal colors for all commands?
- I'm running Mint 18 Mate 64-bit
- 4.4.0-21-generic x86_64
- All my bash scripts begin with #!/bin/bash

lmuserx4849
Level 5
Level 5
Posts: 798
Joined: Wed Dec 17, 2014 2:55 am

Re: Make script output text as default color scheme of terminal

Post by lmuserx4849 » Wed Apr 25, 2018 10:13 pm

linx255 wrote:
Tue Apr 24, 2018 2:17 pm
Oh I didn't expect that to be embedded in any one command. Is there a way to just make a script use the default terminal colors for all commands?
I believe the answer is per command.

One thing you have to be careful about is if you post-process the output of commands, the color escape sequence will be in the data.

If you do a tree -C |hexdump -C|less they'll look like [01;34m..[00m.

- 36.5. "Colorizing" Scripts

User avatar
linx255
Level 5
Level 5
Posts: 663
Joined: Mon Mar 17, 2014 12:43 am

Re: Make script output text as default color scheme of terminal

Post by linx255 » Wed Apr 25, 2018 10:43 pm

Thanks!
- I'm running Mint 18 Mate 64-bit
- 4.4.0-21-generic x86_64
- All my bash scripts begin with #!/bin/bash

lmuserx4849
Level 5
Level 5
Posts: 798
Joined: Wed Dec 17, 2014 2:55 am

Re: Make script output text as default color scheme of terminal

Post by lmuserx4849 » Sat Apr 28, 2018 10:38 pm

linx255 wrote:
Wed Apr 25, 2018 10:43 pm
Thanks!
I was thinking about this a little more. Talking about the output of a single command is one thing, but you can customize the console and output via the console GUI menu and the command prompt PS1. The GUI menu item might be under setting, preferences or profile. If output is hard to read, maybe increasing the font size will help. The command prompt can be set in your $HOME/.bashrc. If you search /etc/bash.bashrc for PS1 you should see some references to it. When you create a new user, files from /etc/skel are used. If you look at /etc/skel/.bashrc you'll also see a reference to PS1.

Please see the attached image and the web page Bash/Prompt customization. There are additional links at the bottom of that page.

There's a good article on console colors here. So if a particular command doesn't have a color option, you could always post process it within a script. You would just embed between the escape sequences: echo -e '\e[1;3;31m Hello world \e[0m'.

From the above web site. See the possibilities:

Code: Select all

#!/bin/bash
for fgbg in 38 48 ; do #Foreground/Background
  for color in {0..256} ; do #Colors
    #Display the color
    echo -en "\e[${fgbg};5;${color}m ${color}\t\e[0m"
    #Display 10 colors per lines
    if [ $((($color + 1) % 10)) == 0 ] ; then
      echo #New line
    fi
  done
  echo #New line
done
Attachments
console-colors.jpg

User avatar
linx255
Level 5
Level 5
Posts: 663
Joined: Mon Mar 17, 2014 12:43 am

Re: Make script output text as default color scheme of terminal [UNSOLVABLE]

Post by linx255 » Sun Apr 29, 2018 2:11 am

Thanks. I'm not so concerned with setting my own color scheme as I am the script's terminal using the default colors assigned to directories, certain file types, etc. So when I run tree from a script it's not all displayed as one color, which is harder to read than when I run tree from the terminal manually.
- I'm running Mint 18 Mate 64-bit
- 4.4.0-21-generic x86_64
- All my bash scripts begin with #!/bin/bash

Post Reply

Return to “Scripts & Bash”