Nadav
Nadav

Reputation: 2717

problem with bash echo function

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

Upvotes: 1

Views: 845

Answers (3)

Dennis Williamson
Dennis Williamson

Reputation: 359905

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

Upvotes: 0

timo
timo

Reputation: 11

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

var="   value"
echo "${var#"${var%%[![:space:]]*}"}"

Upvotes: 1

Greg Hewgill
Greg Hewgill

Reputation: 992787

Try using the printf shell command:

$ printf "%5d %s\n" 1 test
    1 test
$ printf "%5d %s\n" 123 another
  123 another

Upvotes: 5

Related Questions