oracle .bashrc,.bash_profile 和 .bashrc 区别

安装Oracle 11gRAC的时候,文档上配置的是.bashrc参数。之前配置的都是.bash_profile参数。

从网上搜索了一下,他们的区别。 如下:

/etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置.

https://www.cndba.cn/Dave/article/670

/etc/bashrc:为每一个运行bash shell的用户执行此文件.当bash shell被打开时,该文件被读取.

~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次!默认情况下,他设置一些环境变量,执行用户的.bashrc文件.

~/.bashrc:该文件包含专用于你的bash shell的bash信息,当登录时以及每次打开新的shell时,该文件被读取.

~/.bash_logout:当每次退出系统(退出bash shell)时,执行该文件.

另外,/etc/profile中设定的变量(全局)的可以作用于任何用户,而~/.bashrc等中设定的变量(局部)只能继承/etc/profile中的变量,他们是"父子"关系.

~/.bash_profile是交互式、login方式进入bash运行的

~/.bashrc是交互式non-login方式进入bash运行的。

通常二者设置大致相同,所以通常前者会调用后者。

https://www.cndba.cn/Dave/article/670

2.5.1 Initialization FilesContents

The operating system uses a number of variables which are

essential to make programs run. These so called environment

variables modify the way how the shell behaves. For each

shell there is a number of initialization files, which keep variables

and their values. In the following we go through initialization files

for BASH shell.2.5.1.1 .bashrcContents

The .bashrc is a hidden file which resides in user's home directory and in which environment variables and aliases are kept. It determines the behavior of interactive shells. In .bashrc file we place any shell commands that we want executed every time we start up a new shell.

https://www.cndba.cn/Dave/article/670

The content of .bashrc may be something like the following:

# .bashrc

# User specific aliases and functions

# Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrchttps://www.cndba.cn/Dave/article/670

Fi

# User specific environment and startup programs

PATH=$PATH:$ORACLE_HOME/bin:/usr/local/java/bin

CDPATH=$CDPATH:/playgnd/mg/scripts:/~/Scripts

BASH_ENV=$HOME/.bashrc

USERNAME=Ghodrat

HISTSIZE=100

export BASH_ENV USERNAME

https://www.cndba.cn/Dave/article/670

#The following setting prohibits file overwriting

set -o noclobber

#The following setting ignores ctrl+ d and does not

#end the session

set -o ignoreeof

#The following setting let's special characters work as

#expected

set +o noglob

https://www.cndba.cn/Dave/article/670

## some usefull aliases

alias rm='rm -i'

alias cp='cp -i'

alias ls='ls -F'

2.5.1.2 .bash_profile FileContents

The .bash_profile is a hidden file which resides in user's home directory and defines our processing environment when we login. The .bash_profile is executed when we login and, among other details, establishes such things as home directory, search path, etc. The content of .bash_profile may look like the following:

# .bash_profile

# Get the aliases and functions

if [ -f ~/.bashrc ]; then

. ~/.bashrc

fi

# User specific environment and startup programs echo "Running .bash_profile..." date2.5.1.2 .bash_logout FileContents

The .bash_logout file is a hidden file which resides in user'shome directory and is executed when logging out of a linux system running the BASH shell . This file is usually very short, and contains commands users want to execute upon leaving the account. The most popular is the clear command, which clears the screen.

#Running ~/.bash_logout

echo "Running .bash_logout..."

#USERNAME must have been defined as an environment variable

echo Bye $USERNAME

整理自网络

------------------------------------------------------------------------------https://www.cndba.cn/Dave/article/670

Blog:http://blog.csdn.net/tianlesoftware

网上资源:http://tianlesoftware.download.csdn.net

相关视频:http://blog.csdn.net/tianlesoftware/archive/2009/11/27/4886500.aspx

DBA1群:62697716(满); DBA2群:62697977(满)

DBA3群:62697850DBA超级群:63306533;

--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请

版权声明:本文为博主原创文章,未经博主允许不得转载。

<think> 我们正在分析用户提供的.bash_profile文件内容,并解释其含义、作用以及可能的优化点。 文件内容如下: ``` # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin export PATH ``` 根据引用[1][2],我们知道: 1. 用户登录时,会加载.bash_profile文件。 2. 该文件首先检查并加载.bashrc(如果存在),然后设置用户特定的环境变量启动程序。 具体分析每一部分: 1. 注释行: `# .bash_profile` `# Get the aliases and functions` 是注释,说明文件的作用。 2. 加载.bashrc: ```bash if [ -f ~/.bashrc ]; then . ~/.bashrc fi ``` 这段代码检查用户主目录下的.bashrc文件是否存在(`-f`表示存在且为普通文件)。如果存在,则使用`.`命令(等同于`source`)执行该文件。这样做的目的是让登录shell也使用.bashrc中定义的别名(aliases)函数(functions)。因为通常情况下,.bashrc是为交互式非登录shell准备的,而.bash_profile是为登录shell准备的。通过这样的方式,可以确保登录shell也能使用.bashrc中的设置。 3. 设置PATH环境变量: ```bash PATH=$PATH:$HOME/bin export PATH ``` 这里将用户主目录下的`bin`目录(即`$HOME/bin`)添加到PATH环境变量的末尾,然后导出PATH,使其对后续命令子shell生效。 4. 优化建议: 根据引用[2]中的例子,有时我们还需要添加其他目录,例如`$HOME/.local/bin`(用于用户通过pip install --user安装的命令)。因此,可以考虑修改为: ```bash PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH ``` 这样更全面,避免一些用户安装的程序无法在命令行中直接使用。 5. 安全性考虑: 在设置PATH时,我们通常将系统目录放在前面,用户目录放在后面。这里将用户目录附加在系统PATH之后,是合理的。因为如果用户目录中有与系统命令同名的可执行文件,系统命令会优先执行,除非用户使用绝对路径或明确指定路径。但是,如果希望用户目录中的程序优先于系统命令,则可以将用户目录放在前面(例如:`PATH=$HOME/bin:$PATH`),但这样可能带来安全风险(例如,用户可能无意中覆盖了系统命令),所以一般不推荐。 6. 其他环境变量: 除了PATH,根据引用[4](Oracle环境变量设置示例),有时还需要设置其他环境变量(如LD_LIBRARY_PATH等)。但是,这个示例中只设置了PATH,所以如果有其他需求,可以在此处添加。 7. 文件位置加载顺序: 根据引用[3],.bash_profile是用户登录时bash读取的配置文件(针对登录shell),而.bashrc则是在每次打开新的交互式shell时读取。所以,通常将只在登录时需要执行的命令放在.bash_profile中,而将交互式shell的设置(如别名、函数、提示符等)放在.bashrc中。 8. 问题: 该文件内容基本没有问题,但可能不够全面(如缺少$HOME/.local/bin的路径)。另外,如果用户希望在图形界面中启动的程序也能使用这些环境变量,可能需要将设置放在~/.profile中(因为某些图形界面登录时不会读取.bash_profile,而是读取.profile.bashrc)。但注意,引用[3]提到,如果存在.bash_profile,则不会读取.profile(除非在.bash_profile中显式加载.profile)。所以,一种常见的做法是将环境变量设置在.profile中,然后在.bash_profile中加载.profile(但该示例没有这样做)。 9. 优化建议: - 添加$HOME/.local/bin目录到PATH(如上面所述)。 - 如果需要设置其他环境变量,可以在此文件中添加。 - 考虑将环境变量设置部分提取到.profile文件中,并在.bash_profile中加载.profile(如果存在)。例如: .bash_profile: ```bash # 加载.profile(如果存在) if [ -f ~/.profile ]; then . ~/.profile fi # 加载.bashrc(如果存在) if [ -f ~/.bashrc ]; then . ~/.bashrc fi ``` 然后在.profile中设置环境变量。这样可以让其他shell(如dash)也能共享环境变量设置。 不过,由于用户当前系统可能没有.profile,且用户使用的是bash,所以上述修改不是必须的。 10. 总结: 该.bash_profile文件的作用是: - 在登录时加载.bashrc,使得登录shell可以使用.bashrc中的别名函数。 - 将用户个人的bin目录添加到PATH环境变量中。 优化点: - 将`$HOME/.local/bin`也加入PATH(根据引用[2]的例子)。 - 如果用户有特殊需求(如设置其他环境变量),可以在此文件中添加。 因此,我们可以将文件修改为: ```bash # .bash_profile # 加载.profile(可选,根据需要) if [ -f ~/.profile ]; then . ~/.profile fi # 加载.bashrc if [ -f ~/.bashrc ]; then . ~/.bashrc fi # 设置用户特定的环境变量启动程序 # 将用户本地bin目录bin目录添加到PATH PATH=$PATH:$HOME/.local/bin:$HOME/bin export PATH ``` 注意:如果用户没有.profile文件,那么加载.profile的部分不会执行,所以是安全的。 另外,需要注意的是,修改.bash_profile后,需要重新登录或者使用`source ~/.bash_profile`命令使更改立即生效。 最后,根据用户的需求,生成相关问题。 相关问题: 1. 如何让环境变量设置对所有用户生效? 2. 如何设置永久环境变量? 3. .bashrc.bash_profile有什么区别? 4. 为什么在图形界面中设置的环境变量在终端中不生效? 5. 如何添加多个目录到PATH环境变量? </think> ### .bash_profile 文件内容分析 #### 文件内容解析 ```bash # .bash_profile # 获取别名函数 if [ -f ~/.bashrc ]; then . ~/.bashrc fi # 用户特定的环境启动程序 PATH=$PATH:$HOME/bin export PATH ``` #### 各部分作用说明 1. **`.bashrc` 加载机制** ```bash if [ -f ~/.bashrc ]; then . ~/.bashrc fi ``` - `[ -f ~/.bashrc ]`:检查用户主目录下的 `.bashrc` 文件是否存在[^3] - `. ~/.bashrc`:使用 `source` 命令加载 `.bashrc` 中的配置 - **作用**:确保登录时自动加载用户的 bash 配置(别名、函数等)[^1] 2. **PATH 环境变量设置** ```bash PATH=$PATH:$HOME/bin export PATH ``` - `$PATH:$HOME/bin`:将 `~/bin` 目录追加到系统 PATH 末尾 - `export PATH`:使修改后的 PATH 变量在子 shell 中生效 - **作用**:让用户可以在任意目录执行 `~/bin` 中的自定义脚本[^2] #### 潜在优化建议 1. **增强 PATH 设置** ```bash # 添加更多用户级目录(参考引用[2]) PATH=$PATH:$HOME/.local/bin:$HOME/bin ``` - 理由:许多工具(如 Python 的 `pip install --user`)默认安装到 `~/.local/bin` 2. **增加环境变量防护** ```bash # 避免重复添加路径 [[ ":$PATH:" != *":$HOME/bin:"* ]] && PATH="$PATH:$HOME/bin" ``` 3. **添加基础环境变量** ```bash # 设置默认编辑器 export EDITOR=vim # 设置语言环境 export LANG=en_US.UTF-8 ``` 4. **加载系统级配置** ```bash # 加载系统级配置(参考引用[3]) [ -f /etc/profile ] && . /etc/profile ``` #### 最佳实践总结 1. **分层配置原则**: - 系统级设置 → `/etc/profile` - 用户环境变量 → `~/.bash_profile` - Shell 交互设置 → `~/.bashrc`[^3] 2. **安全建议**: ```bash # 限制 root 用户 PATH if [ "$(id -u)" -eq 0 ]; then PATH="/usr/local/sbin:/usr/sbin:/sbin:$PATH" fi ``` 3. **路径顺序**: - 用户目录 (`$HOME/bin`) 应放在**系统目录之后**,避免覆盖系统命令 ### 优化后的完整示例 ```bash # .bash_profile [ -f /etc/profile ] && . /etc/profile # 加载系统配置 # 加载用户bash配置 [ -f ~/.bashrc ] && . ~/.bashrc # 设置PATH(含防重复添加) dirs=( "$HOME/.local/bin" "$HOME/bin" "$HOME/.dotnet/tools" ) for dir in "${dirs[@]}"; do [[ ":$PATH:" != *":$dir:"* ]] && PATH="$PATH:$dir" done export PATH # 设置用户级环境变量 export EDITOR=vim export HISTSIZE=5000 export LANG=en_US.UTF-8 ``` ### 相关问题 1. 如何在 Linux 中设置永久环境变量? 2. `.bashrc` `.bash_profile` 应该分别存放哪些配置?[^3] 3. 为什么在 SSH 登录后环境变量不生效? 4. 如何防止环境变量 PATH 被恶意篡改? 5. 系统重启后环境变量失效的可能原因有哪些?[^1] > 环境变量配置需遵循分层管理原则[^3],用户级设置在登录时加载[^1],路径顺序影响命令优先级[^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值