Question

So I made this file and I am currently running it within a bash shell and it lists fine, however each result is placed on its own individual line. I was wondering how I would make the same program, but make it place the results laterally, one result after another.

x=0;while [ $x -le  256 ]
do
t=$(tput sgr 0);r=$(tput setaf $x);echo "${r} $x ${t}";((x=$x+1))
done
Was it helpful?

Solution

How i understand, u need something like this:

x=0;while [ $x -le  256 ]
do
t=$(tput sgr 0);r=$(tput setaf $x);echo -n "${r} $x ${t}";((x=$x+1))
done

U forgot to use echo -n (no new line)

Formatted version with columns:

#!/bin/bash

t=$(tput sgr 0)
for x in {0..255}; do
  printf "%s%4s${t}" "$(tput setaf $x)" $x
  if [ $((x % 15)) -eq 0 ]; then  echo;  fi;
done

OTHER TIPS

Try less -r or less -R:

From the less man page:

-r or --raw-control-chars
         Causes "raw" control characters to be displayed. The default is to 
         display control characters using the caret notation; for example, a 
         control-A (octal 001) is displayed as "^A". Warning: when the -r 
         option is used, less cannot keep track of the actual appearance of the 
         screen (since this depends on how the screen responds to each type of
         control character). Thus, various display problems may result, such as
         long lines being split in the wrong place.

-R or --RAW-CONTROL-CHARS
         Like -r, but tries to keep track of the screen appearance where possible.
         This works only if the input consists of normal text and possibly some 
         ANSI "color" escape sequences, which are sequences of the form:

             ESC [ ... m

         where the "..." is zero or more characters other than "m". For the 
         purpose of keeping track of screen appearance, all control characters
         and all ANSI color escape sequences are assumed to not move the cursor.
         You can make less think that characters other than "m" can end ANSI 
         color escape sequences by setting the environment variable 
         LESSANSIENDCHARS to the list of characters which can end a color escape 
         sequence.

The command echo, add a new line automatically. If you want to print the output in one line, use printf Command.

To change the color, you can use \e, like this:

echo -e "\e0;31m Your text"

This line will print your text in red color. You can change the number 31.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top