Skip to content

Instantly share code, notes, and snippets.

@Rendevior
Last active December 31, 2021 09:01
Show Gist options
  • Save Rendevior/d08410df173ed2b48d36ff87e212a787 to your computer and use it in GitHub Desktop.
Save Rendevior/d08410df173ed2b48d36ff87e212a787 to your computer and use it in GitHub Desktop.
Easiest way to Center Multiline Text in Bash
#!/bin/bash
function Centered(){
echo "$1" | sed -e :a -e "s/^.\{1,$(tput cols)\}$/ & /;ta" | tr '\n' '\r'
}
# Using Pipe
function CenteredPipe(){
sed -e :a -e "s/^.\{1,$(tput cols)\}$/ & /;ta" /dev/stdin | tr '\n' '\r'
}
@Rendevior
Copy link
Author

Rendevior commented Dec 31, 2021

Output:

~# Centered "Hello World Center"

                          Hello World Center                          

~# echo "Hello World Center" | CenteredPipe

                          Hello World Center                          

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment