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

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
linx255
Level 5
Level 5
Posts: 668
Joined: Mon Mar 17, 2014 12:43 am

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

Post by linx255 »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
- I'm running Mint 18 Mate 64-bit
- 4.15.0-34-generic x86_64
- All my bash scripts begin with #!/bin/bash
User avatar
catweazel
Level 19
Level 19
Posts: 9763
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 »

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
"There is, ultimately, only one truth -- cogito, ergo sum -- everything else is an assumption." - Me, my swansong.
User avatar
linx255
Level 5
Level 5
Posts: 668
Joined: Mon Mar 17, 2014 12:43 am

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

Post by linx255 »

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.15.0-34-generic x86_64
- All my bash scripts begin with #!/bin/bash
lmuserx4849

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

Post by lmuserx4849 »

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: 668
Joined: Mon Mar 17, 2014 12:43 am

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

Post by linx255 »

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

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

Post by lmuserx4849 »

linx255 wrote: Wed Apr 25, 2018 10:43 pmThanks!
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
User avatar
linx255
Level 5
Level 5
Posts: 668
Joined: Mon Mar 17, 2014 12:43 am

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

Post by linx255 »

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.15.0-34-generic x86_64
- All my bash scripts begin with #!/bin/bash
Locked

Return to “Scripts & Bash”