Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Bash 入门 学习笔记

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.   
  4. int main(void) {  
  5.   char *myenvvar=getenv("EDITOR");  
  6.   printf("The editor environment variable is set to %s ",myenvvar);  
  7. }  
没给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 EDITOR5、c语言调用函数,char *envname = getenv("EDITOR");