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
",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 EDITOR5、c语言调用函数,char *envname = getenv("EDITOR");