LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 07-17-2012, 03:46 PM   #1
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 205

Rep: Reputation: 27
Bash script - Center text on screen


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.
 
Old 07-17-2012, 03:50 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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..
 
Old 07-17-2012, 03:52 PM   #3
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Here's some stuff to look over, you will likely have to use the x,y coordinates with tputs to specify where you want it to go...

http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html
 
1 members found this post helpful.
Old 07-17-2012, 03:57 PM   #4
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
With stty size you can get the dimensions of your terminal
Code:
bash-4.2# stty size
41 80
 
Old 07-17-2012, 04:15 PM   #5
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 205

Original Poster
Rep: Reputation: 27
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.
 
Old 07-17-2012, 04:24 PM   #6
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
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.
 
Old 07-17-2012, 06:06 PM   #7
gacanepa
Member
 
Registered: May 2012
Location: San Luis, Argentina
Distribution: Debian
Posts: 205

Original Poster
Rep: Reputation: 27
Quote:
Originally Posted by Kustom42 View Post
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
*********************************************************
 
Old 07-17-2012, 06:18 PM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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.
 
1 members found this post helpful.
Old 07-17-2012, 07:51 PM   #9
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Good point on dialog and nice way to script it, the division method should ensure that it stays centered on most every sized terminal.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script text replacement... matthurne Programming 4 06-07-2011 06:46 PM
Bash script: Scanning for text tboss888 Programming 3 01-27-2009 10:27 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-10-2008 11:13 PM
bash script help to parse out text slack guy Linux - Newbie 3 12-30-2004 08:42 AM
Need help reading text file in bash script scilec Programming 3 11-25-2004 06:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration