Monday, October 26, 2015

Bash alias to copy the current IP to clipboard

This alias uses the one defined in the previous post to copy it to the clipboard.
We grabbed the IP with this alias in your .profile file in your home directory

alias cip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | awk '{print \$2}'"

Now define another one called cipb below it with this code

alias cipb="cip | tr -d '\n' | pbcopy"

So now you should have this in your .profile file
alias cip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | awk '{print \$2}'"
alias cipb="cip | tr -d '\n' | pbcopy"

This uses the pbcopy function to copy the value of your cip alias into the clipboard.
The tr -d '\n' removes the line ending from it. This allows me to use it when pasting it into a command on the terminal.
There is also cbpaste to paste whats in the clipboard. We can do a lot with these two commands, fun to play around with them.

You can find me on twitter @LZAntal