The code below will output whatever in file word by word on the screen. For example:

Hello will be displayed for 1 second and disappear. Then, the next word in the sentence will appear for a second and disappear and so on.

How do I output whatever is being displayed in the middle of the screen?

awk '{i=1; while(i<=NF){ print $((i++)); system("sleep 1; clear") }}' file
  • What is it that you're trying to achieve, exactly? – muru Mar 21 '15 at 18:33
  • that command displays each word form a file in the top left corner of the screen. I need to know how to make the output in the middle of the screen. – Nebelz Cheez Mar 21 '15 at 18:40
  • 4
    Yes, but what are you trying to achieve? This sounds like an XY problem, – muru Mar 21 '15 at 18:41
  • What's the "middle of a screen"? The middle of a terminal? The middle of the actual screen? What if you resize the terminal, do you need this to dynamically place the text in the middle whatever size your terminal has? – terdon Mar 21 '15 at 18:42
  • yes. The middle of terminal. – Nebelz Cheez Mar 21 '15 at 18:43

Here you're a very robust bash script:


## When the program is interrupted, call the cleanup function
trap "cleanup; exit" SIGHUP SIGINT SIGTERM

## Check if file exists
[ -f "$1" ] || { echo "File not found!"; exit; }

function cleanup() {
    ## Restores the screen content
    tput rmcup

    ## Makes the cursor visible again
    tput cvvis

## Saves the screen contents
tput smcup

## Loop over all words
while read line
    ## Gets terminal width and height
    height=$(tput lines)
    width=$(tput cols)

    ## Gets the length of the current word

    ## Clears the screen

    ## Puts the cursor on the middle of the terminal (a bit more to the left, to center the word)
    tput cup "$((height/2))" "$((($width-$line_length)/2))"

    ## Hides the cursor
    tput civis

    ## Prints the word
    printf "$line"

    ## Sleeps one second
    sleep 1

## Passes the words separated by a newline to the loop
done < <(tr ' ' '\n' < "$1")

## When the program ends, call the cleanup function
| improve this answer | |

Try the script below. It will detect the size of the terminal for every input word so will even dynamically update if you resize the terminal while it's running.

#!/usr/bin/env bash

## Change the input file to have one word per line
tr ' ' '\n' < "$1" | 
## Read each word
while read word
    ## Get the terminal's dimensions
    height=$(tput lines)
    width=$(tput cols)
    ## Clear the terminal

    ## Set the cursor to the middle of the terminal
    tput cup "$((height/2))" "$((width/2))"

    ## Print the word. I add a newline just to avoid the blinking cursor
    printf "%s\n" "$word"
    sleep 1

Save it as ~/bin/foo.sh, make it executable (chmod a+x ~/bin/foo.sh) and give it your input file as its first argument:

foo.sh file
| improve this answer | |

bash function to do the same

mpt() { 
   clear ; 
   w=$(( `tput cols ` / 2 ));  
   h=$(( `tput lines` / 2 )); 
   tput cup $h;
   printf "%${w}s \n"  "$1"; tput cup $h;
   sleep 1;

and then

mpt "Text to show"
| improve this answer | |
  • 1
    This seems to be exactly the same as my answer except that it shows one thing and not every word of a sentence read from a file separately as requested by the OP. – terdon Mar 21 '15 at 20:33

Here's Python script that is similar to @Helio's bash solution:

#!/usr/bin/env python
import fileinput
import signal
import sys
import time
from blessings import Terminal # $ pip install blessings

def signal_handler(*args):
    raise SystemExit

for signal_name in "SIGHUP SIGINT SIGTERM".split():
    signal.signal(getattr(signal, signal_name), signal_handler)

term = Terminal()
with term.hidden_cursor(), term.fullscreen():
    for line in fileinput.input(): # read from files on the command-line and/or stdin
        for word in line.split(): # whitespace-separated words
            # use up to date width/height (SIGWINCH support)
            with term.location((term.width - len(word)) // 2, term.height // 2):
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.