Shell中IFS用法

來源:互聯網
上載者:User

標籤:

一 、IFS的介紹   Shell 指令碼中有個變數叫IFS(Internal Field Seprator) ,內部域分隔字元。完整定義是The shell uses the value stored in IFS, which is the space, tab, and newline characters by default, to delimit words for the read and set commands, when parsing output from command substitution, and when performing variable substitution.   Shell 的環境變數分為set, env 兩種,其中 set 變數可以通過 export 工具匯入到 env 變數中。其中,set 是顯示設定shell變數,僅在本 shell 中有效;env 是顯示設定使用者環境變數 ,僅在當前會話中有效。換句話說,set 變數裡包含了env 變數,但set變數不一定都是env 變數。這兩種變數不同之處在於變數的範圍不同。顯然,env 變數的範圍要大些,它可以在 subshell 中使用。   IFS 是一種 set 變數,當 shell 處理"命令替換"和"參數替換"時,shell 根據 IFS 的值,預設是 space, tab, newline 來拆解讀入的變數,然後對特殊字元進行處理,最後重新組合賦值給該變數。二、 IFS的簡單一實例1 查看IFS的值echo "$IFS" echo "$IFS"|od -b0000000 040 011 012 012  0000004直接輸出IFS是看不到值的,轉化為二進位就可以看到了,"040"是空格,"011"是Tab,"012"是分行符號"\n" 。最後一個 012 是因為 echo 預設是會換行的。2 實際中的應用#!/bin/bashOLD_IFS=$IFS #儲存原始值IFS="" #改變IFS的值......IFS=$OLD_IFS #還原IFS的原始值

Shell中IFS用法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.