变量是任何一种编程语言不可缺少的组成部分,在bash中每一个变量值都是字符串。环境变量就是shell环境和操作系统环境用来存储一些特别的值的变量。 查看进程环境变量的方法b@V3000:~$ cat /proc/$PID/environ #$PID 是你要查询的进程的环境变量 b@V3000:~$ pgrep chrome #查看某个进程的pid 2184 2197 2201 2220 2225 2334 2436 3096 3657 3669 查看系统环境变量(全局环境变量)b@V3000:~$ env SHELL=/bin/bash #环境变量通常是key=value形式 ..... #还有很多未贴出来实战演练 如何给变量赋值呢?b@V3000:~$ name=shell #很简单,这样就赋值了。注意这里有个坑name = shell 是比较不是赋值 b@V3000:~$ echo $name #这里如果忘记了echo是做什么的,请爬楼看之前的文章,注意使用变量是前面$不能少 shell b@V3000:~$ 如何打印变量呢?b@V3000:~$ echo $name #方法一 shell b@V3000:~$ echo ${name} #方法二 shell 有没有发现这样在终端中设置变量不是很直观,后期也没法维护。是的,这只是方便演示。 真正的shell脚本是要写到一个以.sh结尾的脚本文件中的请看代码b@V3000:~$ cat script.sh #!/bin/bash title="hello world" num=10 echo "$title $num" b@V3000:~$ chmod u+x script.sh #添加可执行权限 b@V3000:~$ ls -l script.sh -rwxrw-r-- 1 b b 59 10月 7 10:24 script.sh b@V3000:~$ ./script.sh #运行脚本文件 hello world 10 那么环境变量如何设置呢?b@V3000:~$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/b/go/bin:/home/b/gopath/bin b@V3000:~$ cat /etc/profile #/etc/profile配置文件对所有用户生效 # /etc/profile: system-wide .profile file for the Bourne shell (sh(1)) # and Bourne compatible shells (bash(1), ksh(1), ash(1), ...). if [ "${PS1-}" ]; then if [ "${BASH-}" ] && [ "$BASH" != "/bin/sh" ]; then # The file bash.bashrc already sets the default PS1. # PS1="h:w$ " if [ -f /etc/bash.bashrc ]; then . /etc/bash.bashrc fi else if [ "`id -u`" -eq 0 ]; then PS1="# " else PS1="$ " fi fi fi if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi export GOROOT=/home/b/go export GOPATH=/home/b/gopath export PATH=$PATH:$GOROOT/bin:$GOPATH/bin export GO111MODULE=auto export GOPROXY=https://goproxy.cn