I have a VPS and an associated IP address for it. I'm doing a lot of work with it in my bash shell and I'm wondering if there's some way I can create either an alias or a environmental shortcut for the IP address to avoid having to type it every time. How would I do this?


Add to your ~/.bashrc:

export myip=""

Then you can use $myip on the command line after logging in or running source ~/.bashrc:

ping $myip

Aliases only work as commands, so you use it to replace commands which might include your IP:

alias myping='ping'
alias myssh='ssh user@'

export somevarname="someIPaddress" should do what you want. after you do that, you should be able to use $somevarname instead. Note that the export should stay around for your current shell. If you log out, you'll have to set it again on login (or set it in your profile/bashrc)


Assuming you're connecting via ssh:

export foo=`echo $SSH_CONNECTION  | awk '{print $3}'`
echo $foo

This way you don't need to "preset" the IP for each machine. Put that in your .bashrc or .bash_profile or wherever you like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.