Linux bashrc profile分析

介绍

在Linux系统中,我们常需要配置环境变量甚至执行命令,比如,在 /etc/profile 中的 PATH;
在~/.bash_profile 中的 alias。

本文意在介绍不同的文件中命令的执行时机和作用范围。

文件种类

在Linux内核系统中,起相似作用的文件共分为:

  • /etc/profile

  • /etc/bashrc(在Ubuntu系统中为/etc/bash.bashrc)

  • ~/.bash_profile

  • ~/.bashrc

分析

File NameLoginNew bash
/etc/profiletruefalse
/etc/bashrctruetrue
~/.bash_profiletruefalse
~/.bashrctruetrue

扩展

export
  • 申明变量时,不加export只能在本shell文件内引用;加上export后,在本文件执行的所有shell中均可引用。

a.sh:

#!/bin/bash

echo ----- a.sh -----

A=1
export B=2

echo A=$A
echo B=$B


sh ./b.sh

b.sh:

#!/bin/bash

echo ----- b.sh -----

echo A=$A
echo B=$B

sh c.sh

c.sh:

#!/bin/bash

echo ----- c.sh -----

echo A=$A
echo B=$B

        在 a.sh 中申明 A B 两个变量,其中 B 变量使用了 export。b.sh 和 c.sh 仅仅是打印 A B 变量。打印结果如下:

[root@886d89b1d8b4:/tmp# ./a.sh  
----- a.sh -----
A=1
B=2
----- b.sh -----
A=
B=2
----- c.sh -----
A=
B=2

总结:使用 export 申明变量可在 bash 打开的所有 bash 中引用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值