How do I make this python command line an alias in bash?
Question
I want a quick an easy way to check my ip address after reading a recent question that had been answered on SO. For future reference, is there a way to make the following alias work?
alias myip='python -c "from urllib import urlopen; print urlopen("http://whatismyip.appjet.net").read()[:-1]"'
Solution
alias myip="python -c 'from urllib import urlopen; print urlopen(\"http://whatismyip.appjet.net\").read()[:-1]'"
You need to use single quotes inside the alias to stop bash trying to interpret parts of your code inside them. The escapes on the double quotes get stripped out while processing what the alias itself is.
OTHER TIPS
Quote the inside double-quotes:
alias myip='python -c "from urllib import urlopen; print urlopen(\"http://whatismyip.appjet.net\").read()[:-1]"'
could also be done with curl:
alias myip='curl "http://whatismyip.appjet.net"'
or using wget:
alias myip='wget -O - "http://whatismyip.appjet.net" 2>/dev/null'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow