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
3 Answers
Try using the printf shell command:
$ printf "%5d %s\n" 1 test
1 test
$ printf "%5d %s\n" 123 another
123 another
2 Comments
Nadav
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
Greg Hewgill
If you want further help, you're going to have to give an example of what you're seeing.
To trim leading whitespace inside a variable you can use Bash parameter expansion:
var=" value"
echo "${var#"${var%%[![:space:]]*}"}"
1 Comment
Dennis Williamson
You can omit the inner set of quotes:
echo "${var#${var%%[![:space:]]*}}"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