实例分析:Linux 系统 CentOS8 shell 中 set, env, export 区别与使用场景

简介: set , env 和 export 之间的异同,以及使用场景

区别:

命令set,env,export 均在 bash 中执行。
set : 改变 shell 属性和定位参数值; 显示本地变量、当前shell 的变量,包括当前用户变量
export是bash的内建指令;显示和设置环境变量。
VAR=whatever是变量定义的bash语法;
env显示环境变量,显示当前用户变量;本身是一个程序,当env被调用/执行时,实际触发以下过程:

  • 命令env作为一个新的进程被执行

    • env修改环境
    • 调用被用作参数的命令(command),env进程被命令(command)进程取代
      举例:
[arthur@localhost blog]$ env GREP_OPTIONS='-v' grep one test.txt
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
#this is the test file to analyse the difference between env,set and export
line two

上述命令将启动两个新的进程:(i) env 和 (ii) grep (事实上第二个进程会取代第一个进程)。从grep进程的角度来看,命令执行的结果等同于:

$ GREP_OPTIONS='-v' grep one test.txt

然而,如果你在 bash 之外或者不想启动另一个 shell ,可以使用这个习惯用法(例如,当你使用exec()系列函数而不是system()调用时)

场景:

命令grep 使用称为GREP_OPTIONS 的环境变量来设置命令默认选项。

1. 创建测试文件 test.txt
[arthur@localhost blog]$ vi test.txt
[arthur@localhost blog]$ cat test.txt 
#this is the test file to analyse the difference between env,set and export
line one
line two
2. 运行grep 命令
$ grep --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE.
Example: grep -i 'hello world' menu.h main.c
-v, --invert-match        select non-matching lines
[arthur@localhost blog]$ grep one test.txt 
line one
[arthur@localhost blog]$ grep -v one test.txt  
#this is the test file to analyse the difference between env,set and export
line two
3. 使用环境变量设置选项:
3.1 若不使用export命令,设置环境变量时,该变量将不能被继承
[arthur@localhost blog]$ GREP_OPTIONS='-v'
[arthur@localhost blog]$ grep one test.txt 
line one

可以发现,选项 -v 并没有传递给命令grep。
使用场景:当你设置一个变量仅仅是为了供shell使用的时候,你可以用上述变量命名方式。例如,当你仅仅是为了实现以下功能时:for i in *;do, 你并不想使用export $i来设置变量。

3.2 传递变量至特定的命令行环境
[arthur@localhost blog]$ GREP_OPTIONS='-v'  grep one test.txt 
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
#this is the test file to analyse the difference between env,set and export
line two

使用场景: 临时性改变所启动程序的特定实例的环境

3.3 使用export命令使变量可被子程序继承
[arthur@localhost blog]$ export GREP_OPTIONS='-v'
[arthur@localhost blog]$ grep one test.txt 
grep: warning: GREP_OPTIONS is deprecated; please use an alias or script
#this is the test file to analyse the difference between env,set and export
line two

使用场景: 为shell 中随后启动的进程设置变量的最常见方法

拓展: #!/usr/bin/env

env 不需要全路径指向一个程序,是因为它使用execvp()函数,这个函数类似shell的工作原理,通过PATH变量来寻找路径,然后,用命令的运行来取代自身。因此,它可以用于找出解释器(如 perl 或者 python)在路径 中的位置。因而经常使用 #!/usr/bin/env interpreter 而不是 #!/usr/bin/interpreter
此外, 通过修改当前路径,可以影响到具体哪个 python 变量的调用,这将会使如下成为可能:

echo -e `#!/usr/bin/bash\n\necho I am an evil interpreter!` > python
chmod a+x ./python
export PATH=.
python

并不会触发 python 的运行,上述代码将输出结果:

I am an evil interpreter!

reference:
[1] what's the difference between set,export, and env and when should I use each ?.stackExchange.

目录
相关文章
|
Ubuntu Linux Anolis
Linux系统禁用swap
本文介绍了在新版本Linux系统(如Ubuntu 20.04+、CentOS Stream、openEuler等)中禁用swap的两种方法。传统通过注释/etc/fstab中swap行的方式已失效,现需使用systemd管理swap.target服务或在/etc/fstab中添加noauto参数实现禁用。方法1通过屏蔽swap.target适用于新版系统,方法2通过修改fstab挂载选项更通用,兼容所有系统。
1255 3
Linux系统禁用swap
|
Linux
Linux系统修改网卡名为eth0、eth1
在Linux系统中,可通过修改GRUB配置和创建Udev规则或使用systemd链接文件,将网卡名改为`eth0`、`eth1`等传统命名方式,适用于多种发行版并支持多网卡配置。
1847 3
|
安全 Linux iOS开发
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
967 53
Binary Ninja 5.1.8104 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
Linux API iOS开发
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
899 14
Binary Ninja 4.2.6455 (macOS, Linux, Windows) - 反编译器、反汇编器、调试器和二进制分析平台
|
监控 关系型数据库 MySQL
在CentOS系统中,如何统计哪个进程打开了文件描述符?
利用上述方法,你可以有效地监控和统计CentOS系统中的进程打开的文件描述符数量,以帮助排查错误或优化系统配置。通过组合使用各种工具和命令,可以获得对系统状态和行为的深入了解,进而做出相应的调整和
580 5
|
Ubuntu Linux 网络安全
Linux系统初始化脚本
一款支持Rocky、CentOS、Ubuntu、Debian、openEuler等主流Linux发行版的系统初始化Shell脚本,涵盖网络配置、主机名设置、镜像源更换、安全加固等多项功能,适配单/双网卡环境,支持UEFI引导,提供多版本下载与持续更新。
1309 3
Linux系统初始化脚本
|
安全 Linux Shell
Linux系统提权方式全面总结:从基础到高级攻防技术
本文全面总结Linux系统提权技术,涵盖权限体系、配置错误、漏洞利用、密码攻击等方法,帮助安全研究人员掌握攻防技术,提升系统防护能力。
1402 1
|
数据管理 Linux iOS开发
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
Splunk Enterprise 9.4.5 (macOS, Linux, Windows) - 机器数据管理和分析
505 0
|
存储 Ubuntu Linux
VMware-安装CentOS系统教程及安装包
虚拟机相当于是一个独立于你电脑的环境,在这个环境上面,你可以安装Linux、Windows、Ubuntu等各个类型各个版本的系统,在这个系统里面你不用担心有病读等,不用担心文件误删导致系统崩溃。 虚拟机也和正常的电脑系统是一样的,也可以开关机,不用的时候,你关机就可以了,也不会占用你的系统资源,使用起来还是比较方便 这里也有已经做好的CentOS 7系统,下载下来解压后直接用VMware打开就可以使用
1990 69
|
存储 分布式计算 Linux
安装篇--CentOS 7 虚拟机安装
VMware 装 CentOS 7 不知道从哪下手?这篇超详细图文教程手把手教你在 VMware Workstation 中完成 CentOS 7 桌面系统的完整安装流程。从 ISO 镜像下载、虚拟机配置,到安装图形界面、设置用户密码,每一步都有截图讲解,适合零基础新手快速上手。装好之后无论你是要搭 Hadoop 集群,还是练 Linux ,这个环境都够你折腾一整天!
4999 3