Ubuntu 16.04 : Center greeting command in terminal

I've recently added a greeting message (something like 'Hello Alex!') in ~/.bashsrc file. Now everytime I open a terminal, this command will appear first.

My question is : How do I make to center the command, no matter if my terminal is maximized or not?


Solution 1:

One fairly crude way would be to print the string in a field width that uses the value of the terminal's COLUMNS variable e.g.

user@pc:~$ str='Hello Alex!'
user@pc:~$ printf '%*s\n' $(( (COLUMNS+${#str})/2 )) "$str"
                                  Hello Alex!
user@pc:~$

If you want something fancier, there are a number of 'banner' packages available - such as figlet

user@pc:~$ figlet -c "$str"
                  _   _      _ _            _    _           _
                 | | | | ___| | | ___      / \  | | _____  _| |
                 | |_| |/ _ \ | |/ _ \    / _ \ | |/ _ \ \/ / |
                 |  _  |  __/ | | (_) |  / ___ \| |  __/>  <|_|
                 |_| |_|\___|_|_|\___/  /_/   \_\_|\___/_/\_(_)