tags:

views:

88

answers:

3

how do i print using echo in bash so the row wont "jump" abit to the right cause of the length of the Variable can u please help me with a command that do so

+4  A: 

Try using the printf shell command:

$ printf "%5d %s\n" 1 test
    1 test
$ printf "%5d %s\n" 123 another
  123 another
Greg Hewgill
still cause of the first variable the column "jumps" abit to the right the suggestion you gave me helps with the spaces between variables but not ignoring the length of each one of them
Nadav Stern
If you want further help, you're going to have to give an example of what you're seeing.
Greg Hewgill
+1  A: 

To trim leading whitespace inside a variable you can use Bash parameter expansion:

var="   value"
echo "${var#"${var%%[![:space:]]*}"}"
timo
You can omit the inner set of quotes: `echo "${var#${var%%[![:space:]]*}}"`
Dennis Williamson
A: 

Use tabs to separate your columns.

echo -e "$var1\t$var2"

or, better, use printf to do it:

printf "%s\t%s\n" $var1 $var2

Or, as Greg Hewgill showed, use field widths (even with strings - the hyphen makes them left-aligned):

printf "%-6s %-8s %10s\n" abcde fghij 12345
Dennis Williamson