1

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 3

5

Try using the printf shell command:

$ printf "%5d %s\n" 1 test
    1 test
$ printf "%5d %s\n" 123 another
  123 another
Sign up to request clarification or add additional context in comments.

2 Comments

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
If you want further help, you're going to have to give an example of what you're seeing.
1

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

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

1 Comment

You can omit the inner set of quotes: echo "${var#${var%%[![:space:]]*}}"
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.