Trying to adapt the following function to use all arguments rather than just the first one.
The code should introduce nc spaces ahead of the start of each argument passed.
Each shifted argument must be on new line. The first argument is numeric, defining the amount of shift.
#!/bin/bash
shifted-block ()
{
if (( $# == 1 )); then
nc=0 ; arg="$1"
elif (( $# >= 2 )); then
nc="$1" ; arg="$2"
else
nc=0 ; arg="$1"
fi
nw=$(( nc ${#arg} ))
printf "%${nw}s\n" "$arg"
}
CodePudding user response:
Here is a solution :
shifted-block ()
{
local nc=0
if (($# > 1)); then nc=$1 ; shift ; fi
local arg nw
for arg in "$@" ; do
nw=$(( nc ${#arg} ))
printf "%${nw}s\n" "$arg"
done
}
CodePudding user response:
Below is a version without using an explicit loop
shifted-block () {
(( $# > 1 )) &&
printf -v leading_blanks '%*s' "$1" "" &&
shift &&
printf '%s\n' "${@/#/$leading_blanks}"
}