Check string length from terminal
Sometimes at work I need to check some string's length. Here's how to do it from terminal:
printf hanki.dev | wc -c
I use printf
instead of echo
, since echo
adds newline to the end, which increases the length by 1. I've made a command for it to simplify things:
// .zshrc
len() {
printf "$*" | wc -c
}
Now I can just call it with len hanki.dev
🤑 Since "$*"
catches all the arguments, the func works even with sentences with spaces:
len this is a sentence
If you have some special case, like 2 spaces in a row, you can always wrap your string in quotes which makes it safer:
len "this is a sentence"