在阅读了最近在 SO 上回答的问题后,我想要一种快速简单的方法来检查我的 IP 地址。为了将来参考,有没有办法使以下别名起作用?
alias myip='python -c "from urllib import urlopen; print urlopen("http://whatismyip.appjet.net").read()[:-1]"'
alias myip="python -c 'from urllib import urlopen; print urlopen(\"http://whatismyip.appjet.net\").read()[:-1]'"
您需要在别名中使用单引号来阻止 bash 尝试解释其中的部分代码。在处理别名本身的内容时,双引号上的转义符会被去除。
引用内部双引号:
alias myip='python -c "from urllib import urlopen; print urlopen(\"http://whatismyip.appjet.net\").read()[:-1]"'
也可以用 curl 完成:
alias myip='curl "http://whatismyip.appjet.net"'
或使用 wget:
alias myip='wget -O - "http://whatismyip.appjet.net" 2>/dev/null'