tags:

views:

179

answers:

2

I need to create a simple fixed width text file in KShell. My current attempt using printf to pad the string isn't working out very well. What's the shortest, cleanest way to create a fixed width string in shell?

+1  A: 

KSH compresses several spaces into one when it parses certain inputs. So to achieve what you want, you must write the formatted string directly to a file without passing it through any variables. Use printf to format everything in one go and redirect to the file:

printf "%-10s%-5s%-20s\n" $str1 $str2 $str3 >> file
Aaron Digulla
+1  A: 

As I stated in my answer to that question, you need to put quotes around your variables.

TEXT=`padSpaces "TEST" 10`
TEXT="${TEXT}A"
echo ${TEXT}
TEST A
echo "${TEXT}"
TEST          A
Dennis Williamson
Thanks, I've got that now.
C. Ross

related questions