【问题标题】:How do I make this python command line an alias in bash?如何使这个 python 命令行成为 bash 中的别名?
【发布时间】:2010-10-11 16:52:43
【问题描述】:

在阅读了最近在 SO 上回答的问题后,我想要一种快速、简单的方法来检查我的 IP 地址。为了将来参考,有没有办法使以下别名起作用?

alias myip='python -c "from urllib import urlopen; print urlopen("http://whatismyip.appjet.net").read()[:-1]"'

【问题讨论】:

    标签: python bash alias


    【解决方案1】:

    也可以用 curl 来完成:

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

    或使用 wget:

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

    【讨论】:

      【解决方案2】:
      alias myip="python -c 'from urllib import urlopen; print urlopen(\"http://whatismyip.appjet.net\").read()[:-1]'"
      

      您需要在别名中使用单引号来阻止 bash 尝试解释其中的部分代码。在处理别名本身时,双引号上的转义符会被去除。

      【讨论】:

      • 刮一下,一氧化碳先进入!
      【解决方案3】:

      引用里面的双引号:

      alias myip='python -c "from urllib import urlopen; print urlopen(\"http://whatismyip.appjet.net\").read()[:-1]"'
      

      【讨论】:

      • 你们两个同时回答,但 kevinbutler 的代表较少。答案交给小家伙。谢谢你们俩。
      猜你喜欢
      • 2016-07-14
      • 2013-04-23
      • 2010-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多