Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all,
I would like to know if it is possible to center some text on the screen using a bash script.
Example:
Code:
#!/bin/bash
echo "Centered text"
I can add spaces before and after the text string to try to manually center it (but this would sort of work only for a certain screen resolution), but it won't look as good as if there is something that can be inserted in the code that will work with all screen resolutions.
Suggestions are welcome! Thanks in advance.
You may be able to use tputs for this, tputs is very powerful in regards to terminal control. I have never used it for this purpose but reviewing some tputs tutorials may help you.
I'm going to do some searching and see if I can find you the answer. I have used tputs previously to move the cursor to top left of terminal, then back down to bottom right, etc..
I will look up the information you guys just shared! Thanks a lot for taking the time to write.
Nevertheless, just playing with coordinates will not quite work in all instances.
I found the following in YouTube:
1) Assign a text string to a variable, let's say title="Hello world!"
2) printf "%*s\n" $(((${#title}+$COLUMNS)/2)) "$title" will output
Hello world! centered in the screen, regarding of its resolution.
That print statement also has its limitations in the fact that you are using columns. Using perl or something more robust than BASH would allow you to create the script to always display the same way, regardless of the size of the terminal.
That print statement also has its limitations in the fact that you are using columns. Using perl or something more robust than BASH would allow you to create the script to always display the same way, regardless of the size of the terminal.
You were right about that my friend. The printf statement does have its limitations... that sample script only works in the terminal but it doesn't inside a shell script. The following script that uses tput does the job:
Code:
#!/bin/bash -i
#Menú de programa centrado
titulo="Menú principal"
clear
ast="*********************************************************"
columnas=$(tput cols)
y=$((($columnas-${#ast})/2))
x=0
tput clear
tput cup $x $y
echo "${ast}"
echo ""
y=$((($columnas-${#titulo})/2))
x=1
tput cup $x $y
echo "${titulo}"
echo ""
y=$((($columnas-${#ast})/2))
x=2
tput cup $x $y
echo "${ast}"
Prints the following, centered in the screen:
*********************************************************
Menú principal
*********************************************************
If you want to put formatted text, like menus, textboxes, etc.. on the screen then maybe dialog is an option for you, it also simplifies the input handling.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.