Baoky chen
Baoky chen

Reputation: 183

Shell programing printf %s

need some help on using printf %s

This is part of my code

getspace=`expr 50-$getlength`;

#the space is between var1 & var2 
printf "%s %20s\n" "$var1" "$var2"

I want swap the value %20s with getspace result.. means if getspace is 15 for this loop it will be %15s, and if getspace is 50 for next element, it willbe %50s

How do i swap in the dynamic variable for printf

I tried and it doesnt work

printf "%s %$getspaces\n" "$var1" "$var2"

Upvotes: 0

Views: 1934

Answers (3)

user1800354
user1800354

Reputation: 66

I think that the line

getspace=`expr 50-$getlength`

should be

getspace=`expr 50 - $getlength`

and then,

printf "%s %${getspace}s\n" "$var1" "$var2"

should work, as it has worked for me.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

printf "%s %*s\n" "$var1" "$getspace" "$var2"

Upvotes: 0

codable
codable

Reputation: 6529

printf "%s %${getspace}s\n" "$var1" "$var2"

Upvotes: 1

Related Questions