先給大家介紹下linux中echo命令的使用
echo是打印變量的值或者給定的字符串,
比如,輸入echo hello或者echo "hello"都是在控制臺打印出hello單詞
但是我們需要把打印出來的字符串記錄到文本文件中,就需要>和>>命令
touch a.txt 新建一個文本文件a.txt
echo hello > a.txt
則a.txt中會記錄下hello,但是如果再次執行echo hello > a.txt。則會覆蓋之前的hello,
怎樣追加呢?需要>>命令
echo world >> a.txt 則a.txt中會記錄的是hello word,但是hello和word不是寫在一行的,
而是每個單詞占用一行的。
再比如 echo $HOME 控制臺則會打印出當前用戶的根路徑/home/picc4
下面通過代碼介紹下Linux echo命令的三種實現方式
1:
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.// License: https://creativecommons.org/licenses/by-nc-sa/4.0/package mainimport ( "fmt" "os")func main() { var s, sep string for i := 1; i < len(os.Args); i++ { s += sep + os.Args[i] sep = " " } fmt.Println(s)}
2:
package mainimport ( "fmt" "os")func main() { s, sep := "", "" for _, arg := range os.Args[1:] { s += sep + arg sep = " " } fmt.Println(s)}
3:
package mainimport ( "fmt" "os" "strings")//!+func main() { fmt.Println(strings.Join(os.Args[1:], " "))}
總結
以上所述是小編給大家介紹的Linux echo命令的使用及三種實現方式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
新聞熱點
疑難解答