Bash環境變量賦值
- $ myvar='This is my environment variable!'
輸出
- $ echo $myvar
- This is my environment variable!
分割
- $ echo foo${myvar}bar
- fooThis is my environment variable!bar
寫個c程序交互一下,用c語言來讀取環境變量~~
- #include <stdio.h>
- #include <stdlib.h>
-
- int main(void) {
- char *myenvvar=getenv("EDITOR");
- printf("The editor environment variable is set to %s\n",myenvvar);
- }
沒給EDITOR幅值,當然就不會顯示啦~~
- $ ./myenv
- The editor environment variable is set to (null)
賦值了啊。。為啥還不顯示呢??
- $ EDITOR=xemacs
- $ ./myenv
- The editor environment variable is set to (null)
原來要export一下,哈哈,這下出來了
- $ export EDITOR
- $ ./myenv
- The editor environment variable is set to xemacs
這裡要注意,unset之後,就算再export,那個變量也沒掉的了。需要重新幅值
- $ unset EDITOR
- $ ./myenv
- 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");