歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Bash 入門 學習筆記

Bash 入門 學習筆記

日期:2017/3/1 10:12:07   编辑:Linux編程
Bash環境變量賦值
  1. $ myvar='This is my environment variable!'

輸出

  1. $ echo $myvar
  2. This is my environment variable!

分割

  1. $ echo foo${myvar}bar
  2. fooThis is my environment variable!bar

寫個c程序交互一下,用c語言來讀取環境變量~~

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main(void) {
  4. char *myenvvar=getenv("EDITOR");
  5. printf("The editor environment variable is set to %s\n",myenvvar);
  6. }

沒給EDITOR幅值,當然就不會顯示啦~~

  1. $ ./myenv
  2. The editor environment variable is set to (null)

賦值了啊。。為啥還不顯示呢??

  1. $ EDITOR=xemacs
  2. $ ./myenv
  3. The editor environment variable is set to (null)

原來要export一下,哈哈,這下出來了

  1. $ export EDITOR
  2. $ ./myenv
  3. The editor environment variable is set to xemacs

這裡要注意,unset之後,就算再export,那個變量也沒掉的了。需要重新幅值

  1. $ unset EDITOR
  2. $ ./myenv
  3. The editor environment variable is set to (null)
總結一下:

1、賦值,EDITOR="vim",注意"="旁邊不能有空格啊~

2、調用,$EDITOR 或者 ${EDITOR}

3、export EDITOR

4、unset EDITOR

5、c語言調用函數,char *envname = getenv("EDITOR");

Copyright © Linux教程網 All Rights Reserved