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

phy*_*ael 1 python bash alias

在阅读了最近在SO上回答的问题之后,我想快速简单地检查我的IP地址.为了将来参考,有没有办法使以下别名工作?

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

Mat*_*ley 7

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

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


Kev*_*ler 6

引用里面的双引号:

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


小智 5

也可以用curl完成:

alias myip='curl "http://whatismyip.appjet.net"'
Run Code Online (Sandbox Code Playgroud)

或使用wget:

alias myip='wget -O - "http://whatismyip.appjet.net" 2>/dev/null'
Run Code Online (Sandbox Code Playgroud)