Issue
I'm trying to add x
number of 0
at the beginning of a string, the number of characters missing is stored on $x
, but how can I add them into the string?
echo "$exampleString" | sed -i 's/^/0/'
sed
works just fine, but the number of 0
must be typed manually instead of getting from a calculated variable
Solution
You could use printf as such:
x=5
string="test"
printf '%0*d%s\n' "$x" 0 "$string"```
Answered By - user20248374 Answer Checked By - Robin (WPSolving Admin)