简体   繁体   English

如何使这个 python 命令行成为 bash 中的别名?

[英]How do I make this python command line an alias in bash?

I want a quick an easy way to check my ip address after reading a recent question that had been answered on SO.在阅读了最近在 SO 上回答的问题后,我想要一种快速简单的方法来检查我的 ip 地址。 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]"'
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.您需要在别名中使用单引号来阻止 bash 尝试解释其中的部分代码。 The escapes on the double quotes get stripped out while processing what the alias itself is.在处理别名本身的内容时,双引号上的转义符会被删除。

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:也可以用 curl 完成:

alias myip='curl "http://whatismyip.appjet.net"'

or using wget:或使用 wget:

alias myip='wget -O - "http://whatismyip.appjet.net" 2>/dev/null'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM