I am trying to implement a shell function to print a specific number of "space" characters, to provide padding for fixed-width console output.
#!/bin/bash
function printWhiteSpaces
{
local i="122"
local len="$1"
local whitespace=' '
local k=`expr $i - $len`
while [ $k -gt 0 ]
do
echo "${whitespace}"
((k--))
done
}
The above function takes in a single argument as a "length of something", then I take the difference of the desired field width (122) and the argument $1
. And I wish to print that many whitespace characters.
Why won't the above print any whitespaces? If anyone has a solution or an improvement to the above function please share. In particular I want to know if there's a better way to write this function that prints concatenated whitespace characters.
Solution so far:
#!/bin/bash
function printWhiteSpaces
{
local i="122"
local len="$1"
local k=`expr $i - $len`
local whitespace=' '
while [ $k -gt 0 ]
do
echo -n "${whitespace}"
((k--))
done
}