brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: stty size gives scripts the rows and columns of your terminal

Terminal
~/tips/tip-terminal-stty-size-rows-columns

read -r rows cols <<< "$(stty size)"

Prints the number of rows and columns of the current terminal window.

A script that draws a line across the screen needs to know how wide the screen is.

read -r rows cols <<< "$(stty size)"
printf '%s rows, %s columns\n' "$rows" "$cols"
tput cols

stty size prints rows first and columns second, and reads them from the terminal on standard input. tput cols and tput lines give single values. In bash, the variables $COLUMNS and $LINES also hold them in interactive shells. stty size fails with an error when standard input is not a terminal, for example in a pipe or a cron job, so check the exit status in scripts.

Further reading: stty manual page

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link