shell學習之變數

來源:互聯網
上載者:User

標籤:shell   變數

一 變數定義
1 顯式定義 variable=XXX   沒有空格
2 read variable讀取
主要寫了如下兩行代碼,讀取name和namefamily
read -p "please input your name:" name
read -p "please input your whole name :"name family
運行下
[[email protected] shell]# ./var.sh
please input your name:liuliancao
your name is :liuliancao
please input your name:liu liancao
your name is :liu liancao
please input your whole name :liancao liu
your name is liancao,your family is liu
please input your whole name :liancao
your name is liancao,your family is
please input your whole name :liancao l iu
your name is liancao,your family is l iu
可見變數個數和所輸入的個數一樣,則分配正好,如果變數個數多了,則後面的變數為空白,變數個數少了,那麼最後面的是老大(吃最多)。
3 從檔案中讀取
[[email protected] shell]# read message < txt
[[email protected] shell]# echo $message
hello,i amliuliancao由於是文字重新導向,且txt文本行間有分行符號,所以此時只有一行,如果想持續就得使用迴圈
[[email protected] shell]# while read message
> do
> echo $message
> done < txt
hello,i am liuliancao
this is a test
4 命令替換

      1 #!/bin/bash
      2 #
  time=`date +%c`
      3 echo "now time is $time"
  執行結果
[[email protected] shell]# ./com.sh
now time is Sat 09 May 2015 12:26:49 PM CST
5 位置參數
常見的有
$0 完整檔案名稱 basename可以取基本名字
$1 第一個變數內容,依次類推
[email protected] 所有變數以數組方式存 $* 所有變數,歸為大字串
6 刪除變數
unset 變數名稱


shell學習之變數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.