簡體   English   中英

如何使這個 python 命令行成為 bash 中的別名?

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

在閱讀了最近在 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'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM