source命令遇到的一些问题

本文探讨了在Linux环境下使用source命令加载.bashrc文件时遇到的filenotfound错误,解析了source命令的工作原理,特别是在不同shell环境下的行为差异,并提供了有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用source filename命令的时候遇到了一个问题,我使用下面的命令,却报错了

summer@deyuan-server3:~$ source .bashrc
-sh: source: .bashrc: file not found

.bashrc文件就在当前目录下,怎么会 file not found?一开始我以为是权限的问题,因为用ls -l查看了.bashrc的权限,发现是没有执行的权限的,于是加了执行权限,但是还是不行。
然后,我尝试使用相对路径来执行,就成功了。

source ./.bashrc

为什么使用相对路径就可以呢?
查找了source的使用说明,

source filename [arguments]
Read and execute commands from filename in the current shell environment and return the exit status of the last command exe-cuted from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing file-name. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the source path option to the shopt builtin command is turned off, the PATH is not searched. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.

从上面可以知道,如果filename里面不包括“/”,那么source会从PATH路径里搜索文件,当bash不在posix模式才会搜索当前目录。
难道我的shell是在POSIX模式吗?什么是POSIX模式?百度一下,也没找到解释,我的认知里,POSIX是一种通用的标准。
虽然还不明白posix mode,但是找到了解决问题的方法:为执行文件加上相对路径或者绝对路径,或者把执行文件所在的目录添加到PATH里
但是,我还有个疑惑,我的印象里,之前我用source命令的时候好像没有加路径,于是我又在另一台Linux上试了一下,不加路径也能够成功执行。同样都是Ubuntu系统,怎么要求不一样呢?
会不会是shell的原因呢?于是,我查看了两台电脑的shell,

summer@deyuan-server3:~$ echo $0
-sh
ubuntu@ip-172-31-30-180:~$ echo $0
-bash

果然,两台电脑的shell不同,报错的那台用的是sh,另一台是bash,于是我改成了bash,果然就OK了

summer@deyuan-server3:~$ bash
summer@deyuan-server3:~$ echo $0
bash
summer@deyuan-server3:~$ source .bashrc
summer@deyuan-server3:~$ 

Shell的两种主要语法类型有Bourne 和C ,这两种语法彼此不兼容,Bourne家族主要包括sh,ksh,Bash,psh,zsh,C家族主要包括csh,tcsh
Bash与sh兼容,Linux一般都是使用Bash作为用户的基本Shell。
Linux支持的Shell我们可以通过查看/etc/shells文件进行查看

summer@deyuan-server3:~$ cat /etc/shells 
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash

我是用的ssh登录的Linux,为什么默认就是使用的sh呢?
可以查看/etc/passwd文件,在创建用户的时候我的shell类型是sh,所以登录后默认是shell

summer@deyuan-server3:~$ cat /etc/passwd | grep "summer"
summer:x:1007:1007::/home/summer:/bin/sh

用chsh -s /bin/bash命令,把登录shell类型改为bash。如果是管理员,可以直接更改/etc/passwd文件,下次再创建新用户的时候可以指定shell类型(因为默认是用sh),useradd -s /bin/bash {用户昵称}

summer@deyuan-server3:~$ chsh -s /bin/bash
Password: 
summer@deyuan-server3:~$ cat /etc/passwd | grep "summer"
summer:x:1007:1007::/home/summer:/bin/bash
summer@deyuan-server3:~$ 
### Ubuntu 中 `source` 命令的使用说明 #### 1. **定义与作用** `source` 是 Bash Shell 的内置命令,其主要功能是让当前 Shell 解释器读取并执行指定文件中的所有语句[^2]。通过该命令,用户可以在不退出当前 Shell 环境的情况下加载配置文件或脚本的内容。 #### 2. **别名** 除了 `source` 关键字外,还可以使用点号 (`.`) 来实现相同的功能。这是从 Bourne Shell 继承来的特性[^2]。因此,以下两种写法等价: ```bash source filename . filename ``` #### 3. **适用场景** `source` 命令常用于重新加载刚刚修改过的初始化文件(如 `.bashrc` 或其他环境变量设置文件),从而无需注销再重新登录即可使更改即时生效。例如,在编辑了 `.bashrc` 文件之后,可以通过如下方式应用新的配置: ```bash source ~/.bashrc ``` #### 4. **工作原理** 当运行 `source` 命令时,Shell 不会在子进程中执行目标文件,而是直接在当前 Shell 进程中解析和执行其中的指令。这意味着任何由被源文件改变的状态都会影响到调用它的父进程[^1]。 #### 5. **注意事项** 需要注意的是,默认情况下 Ubuntu 使用 Dash 而非 Bash 作为系统的默认 Shell,Dash 更加轻量级且严格遵循 POSIX 标准,这可能导致某些依赖于特定 Bash 功能的脚本无法正常运作[^1]。如果遇到这种情况,则可能需要显式切换至 Bash 执行这些操作或者调整系统默认 Shell 设置为 Bash。 #### 示例代码展示如何创建简单的测试脚本来验证 source 行为: ```bash #!/bin/bash echo "This is sourced script" export TEST_VAR="Test Value" ``` 保存上面内容到名为 test_source.sh 的文件里,并给予可执行权限后就可以这样去尝试它的工作效果了: ```bash chmod +x ./test_source.sh source ./test_source.sh echo $TEST_VAR ``` 以上应该输出 “Test Value”。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值