LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem with space padding (https://www.linuxquestions.org/questions/linux-newbie-8/problem-with-space-padding-4175482676/)

waddles 10-29-2013 02:49 PM

Problem with space padding
 
I "thought" this was how to pad spaces in front of a line:
spaces=" "
pdl=12
pad=`echo "$spaces" | cut -c $pdl`
echo "B${pad}E"
but I get:
B E
I've tried with a constant instead of "$pdl" and get the same thing.
I "THINK" I have used this before but I am not able to get the proper pad length created. Use of printf had similar problems. Does anyone see my mistake. I would like to find a correction rather than an alternate padding solution since I feel certain I've used this before.
Thanks

Firerat 10-29-2013 03:18 PM

use printf

Code:

printf "B%12sE\n"
or for more fun
Code:

printf "%*s%*s%s\n" $(( ${COLUMNS} / 2 )) "B" $((12+1)) "E"
centre(ish)
Code:

Centre="This is in centre"
printf "%*s\n" $(( (${COLUMNS}+${#Centre}) / 2 )) "$Centre"



All times are GMT -5. The time now is 11:15 AM.