2

I am trying to print lines in a BASH script with printf. I know that "%60s" aligns the text but for some reason I get this output:

Welcome root! Please select an option: [1] Install startup repos [2] Update sources.list [3] Edit sources.list [4] Reset sources.list 

Here is my script:

 function main () { main_header printf "%20s\n" "${YELLOW}Welcome ${USER}! Please select an option:" printf "%60s\n" "${GREEN}[1] ${YELLOW}Install new sources.list" printf "%60s\n" "${GREEN}[2] ${YELLOW}Update sources.list" printf "%60s\n" "${GREEN}[3] ${YELLOW}Edit sources.list" printf "%60s\n" "${GREEN}[4] ${YELLOW}Reset sources.list" } main 

Does anyone have any ideas?

3 Answers 3

2

Those are aligned: each line is 60 characters long justified to the right. Try using "%-60s" instead to justify to the left.

Sign up to request clarification or add additional context in comments.

9 Comments

Would this align everything with the first line (Welcome root! Please select an option:) ?
It should do, if I understand what you mean.
Thats useful in some cases. But in my case I want to align the text so its under P"lease select".
Oh okay, try using just "%17s" without the "\n". However this depends on the length of the username so you need to take that into consideration or just accept it as it is.
Hmm okay scrap that, how about printf "%17s%s\n" "${GREEN}[4] ${YELLOW}Reset sources.list" "\n"?
|
1

For me it prints out right aligned. To left align it use %-, for example:

function main () { main_header printf "%-20s\n" "${YELLOW}Welcome ${USER}! Please select an option:" printf "%-60s\n" "${GREEN}[1] ${YELLOW}Install new sources.list" printf "%-60s\n" "${GREEN}[2] ${YELLOW}Update sources.list" printf "%-60s\n" "${GREEN}[3] ${YELLOW}Edit sources.list" printf "%-60s\n" "${GREEN}[4] ${YELLOW}Reset sources.list" } 

This prints

Welcome ! Please select an option: [1] Install new sources.list [2] Update sources.list [3] Edit sources.list [4] Reset sources.list 

(Excluding an error message about main_header: command not found)

To align subsequent lines under "Please", determine the length of indentation, create a variable with a format string that indents by that length and use that format string in printf statements of lines to be indented. The trick with the format string is it first writes an empty string to get the indentation equal to the field width, then it writes the string you want on the same line. For example,

indentString="${YELLOW}Welcome ${USER}! " indentLength=${#indentString} indentFormat="%${indentLength}s%s\n" printf "$indentFormat" "" "${GREEN}[1] ${YELLOW}Install new sources.list" 

Putting this in main() it becomes:

function main () { indentString="${YELLOW}Welcome ${USER}! " indentLength=${#indentString} indentFormat="%${indentLength}s%s\n" main_header printf "%-20s\n" "${YELLOW}Welcome ${USER}! Please select an option:" printf "$indentFormat" "" "${GREEN}[1] ${YELLOW}Install new sources.list" printf "$indentFormat" "" "${GREEN}[2] ${YELLOW}Update sources.list" printf "$indentFormat" "" "${GREEN}[3] ${YELLOW}Edit sources.list" printf "$indentFormat" "" "${GREEN}[4] ${YELLOW}Reset sources.list" } 

which prints:

Welcome training! Please select an option: [1] Install new sources.list [2] Update sources.list [3] Edit sources.list [4] Reset sources.list 

2 Comments

Would it be possible to align the options under "Please select"?
Yes, added a solution to this in my answer. The main thing to keep in mind is that in a printf format specifier such as "%xs" x is an integer specifying the field width and not the absolute offset of the start of the string. See linux.die.net/man/3/printf
0

I like indenting using pr. Sometimes I use remote_function | tr -pro 3 to see the remote output clearly.
In your case using pr will result in the following code:

function options () { echo "${GREEN}[1] ${YELLOW}Install new sources.list" echo "${GREEN}[2] ${YELLOW}Update sources.list" echo "${GREEN}[3] ${YELLOW}Edit sources.list" echo "${GREEN}[4] ${YELLOW}Reset sources.list" } function main () { printf "%20s\n" "${YELLOW}Welcome ${USER}! Please select an option:" options | pr -tro 17 } main 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.