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
2010-08-08 06:52:53
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
2010-08-08 07:10:31
If you want further help, you're going to have to give an example of what you're seeing.
Greg Hewgill
2010-08-08 07:37:09
+1
A:
To trim leading whitespace inside a variable you can use Bash parameter expansion:
var=" value"
echo "${var#"${var%%[![:space:]]*}"}"
timo
2010-08-08 09:07:14
You can omit the inner set of quotes: `echo "${var#${var%%[![:space:]]*}}"`
Dennis Williamson
2010-08-08 12:56:17
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
2010-08-08 13:04:54