TL;DR:: bash/zsh parameter expansion is faster than cut.
which is faster?
Consider you want to take out hostname from URL or IP with port like some.mysql.server:3306
or 192.168.1.10:3306
using bash/zsh.
There are some way to do this.
The first way is using cut
command:
|
|
Now printed 192.168.1.10
on your screen.
The second way is using “shell parameter expansion”, which is functions of bash/zsh built-in. You can use shell parameter expansion like below:
|
|
Printed 192.168.1.10
too.
Which is faster, using cut or shell parameter expansion? I make a small benchmark.
|
|
Wow! In this situation, shell parameter expansion is faster than cut command solution!
Because this is very simple situation, shell parameter expantion is not always better than cut command. However, situations like this example, we should use shell parameter expansion.