1

Any idea how I can still include empty space in a row using printf ie when the column variable is empty? For example:

printf $GREEN"################################ SOURCED ###############################"$RESET
printf $GREEN'%-1s%-15s%-60s%-1s%s\n' '#'$RESET ' REGION NAME:' $OS_REGION_NAME $GREEN'#'$RESET
printf $GREEN"########################################################################"$RESET

Basically I am creating a box with that shows sourced variables. Sometimes some of the fields are empty. The box shouldn't collapse on the right when this happens. I thought I had this down correctly with my column settings on that middle line, but apparently it ignores the width if the column is empty.

The output should look something like this:

################################ SOURCED ###############################
# AUTH URL: blah blah blah                                             #
# REGION NAME: fcdnjcndkjcndkajcndkjcnklcnklsncklsnkcldnslc            #
########################################################################            

but it ends up looking like this

################################ SOURCED ###############################
# AUTH URL: blah blah blah #
# REGION NAME: fcdnjcndkjcndkajcndkjcnklcnklsncklsnkcldnslc #
########################################################################   

SOLUTION

The solution was to put the variables inside double quotes. Such a simple fix...

printf $GREEN'%-1s%-15s%-55s%-1s%s\n' '#'$RESET ' REGION NAME:' "$OS_REGION_NAME" $GREEN'#'$RESET

NOTE

The $GREEN and $RESET was for coloring set in another part of script.

4
  • 1
    Quote your variable expansions. If $OS_REGION_NAME is empty then the variable value is the empty string and the command becomes printf format '#reset' 'green#reset' i.e. you lose an argment. Commented Jul 8, 2015 at 13:40
  • Well that fixes the printf issue, but breaks the variables Commented Jul 8, 2015 at 13:42
  • Also there's no reason to use your $GREEN and $RESET variables in the format string on the first and third lines and no reason to use double quotes on those lines either (not that they hurt anything either). Commented Jul 8, 2015 at 13:42
  • The single quotes were example not literal (that's why I used green and reset and not $GREEN and $RESET. Use double quotes around any variable expansions. Commented Jul 8, 2015 at 13:43

1 Answer 1

1

The problem is that you failed to quote the expansion of $OS_REGION_NAME so when it is the empty string that entire argument to printf disappears (i.e. printf sees one fewer argument and everything after it slides up in the format string specifiers).

That being said I would also rewrite your lines this way

printf '################################ SOURCED ###############################\n' "$GREEN" "$RESET"
printf '$s%-1s%-15s%-60s%-1s%s\n' "$GREEN" "#$RESET" ' REGION NAME:' "$OS_REGION_NAME" "$GREEN#$RESET"
printf '%s########################################################################%s\n' "$GREEN" "$RESET"
Sign up to request clarification or add additional context in comments.

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.