mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translated
This commit is contained in:
parent
4cceec361f
commit
0480c27ba1
@ -1,132 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Import functions and variables into Bash with the source command)
|
||||
[#]: via: (https://opensource.com/article/20/6/bash-source-command)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
Import functions and variables into Bash with the source command
|
||||
======
|
||||
Source is like a Python import or a Java include. Learn it to expand
|
||||
your Bash prowess.
|
||||
![bash logo on green background][1]
|
||||
|
||||
When you log into a Linux shell, you inherit a specific working environment. An _environment_, in the context of a shell, means that there are certain variables already set for you, which ensures your commands work as intended. For instance, the [PATH][2] environment variable defines where your shell looks for commands. Without it, nearly everything you try to do in Bash would fail with a **command not found** error. Your environment, while mostly invisible to you as you go about your everyday tasks, is vitally important.
|
||||
|
||||
There are many ways to affect your shell environment. You can make modifications in configuration files, such as `~/.bashrc` and `~/.profile`, you can run services at startup, and you can create your own custom commands or script your own [Bash functions][3].
|
||||
|
||||
### Add to your environment with source
|
||||
|
||||
Bash (along with some other shells) has a built-in command called `source`. And here's where it can get confusing: `source` performs the same function as the command `.` (yes, that's but a single dot), and it's _not_ the same `source` as the `Tcl` command (which may come up on your screen if you type `man source`). The built-in `source` command isn't in your `PATH` at all, in fact. It's a command that comes included as a part of Bash, and to get further information about it, you can type `help source`.
|
||||
|
||||
The `.` command is [POSIX][4]-compliant. The `source` command is not defined by POSIX but is interchangeable with the `.` command.
|
||||
|
||||
According to Bash `help`, the `source` command executes a file in your current shell. The clause "in your current shell" is significant, because it means it doesn't launch a sub-shell; therefore, whatever you execute with `source` happens within and affects your _current_ environment.
|
||||
|
||||
Before exploring how `source` can affect your environment, try `source` on a test file to ensure that it executes code as expected. First, create a simple Bash script and save it as a file called `hello.sh`:
|
||||
|
||||
|
||||
```
|
||||
#!/usr/bin/env bash
|
||||
echo "hello world"
|
||||
```
|
||||
|
||||
Using `source`, you can run this script even without setting the executable bit:
|
||||
|
||||
|
||||
```
|
||||
$ source hello.sh
|
||||
hello world
|
||||
```
|
||||
|
||||
You can also use the built-in`.` command for the same results:
|
||||
|
||||
|
||||
```
|
||||
$ . hello.sh
|
||||
hello world
|
||||
```
|
||||
|
||||
The `source` and `.` commands successfully execute the contents of the test file.
|
||||
|
||||
### Set variables and import functions
|
||||
|
||||
You can use `source` to "import" a file into your shell environment, just as you might use the `include` keyword in C or C++ to reference a library or the `import` keyword in Python to bring in a module. This is one of the most common uses for `source`, and it's a common default inclusion in `.bashrc` files to `source` a file called `.bash_aliases` so that any custom aliases you define get imported into your environment when you log in.
|
||||
|
||||
Here's an example of importing a Bash function. First, create a function in a file called `myfunctions`. This prints your public IP address and your local IP address:
|
||||
|
||||
|
||||
```
|
||||
function myip() {
|
||||
curl <http://icanhazip.com>
|
||||
|
||||
ip addr | grep inet$IP | \
|
||||
cut -d"/" -f 1 | \
|
||||
grep -v 127\\.0 | \
|
||||
grep -v \:\:1 | \
|
||||
awk '{$1=$1};1'
|
||||
}
|
||||
```
|
||||
|
||||
Import the function into your shell:
|
||||
|
||||
|
||||
```
|
||||
`$ source myfunctions`
|
||||
```
|
||||
|
||||
Test your new function:
|
||||
|
||||
|
||||
```
|
||||
$ myip
|
||||
93.184.216.34
|
||||
inet 192.168.0.23
|
||||
inet6 fbd4:e85f:49c:2121:ce12:ef79:0e77:59d1
|
||||
inet 10.8.42.38
|
||||
```
|
||||
|
||||
### Search for source
|
||||
|
||||
When you use `source` in Bash, it searches your current directory for the file you reference. This doesn't happen in all shells, so check your documentation if you're not using Bash.
|
||||
|
||||
If Bash can't find the file to execute, it searches your `PATH` instead. Again, this isn't the default for all shells, so check your documentation if you're not using Bash.
|
||||
|
||||
These are both nice convenience features in Bash. This behavior is surprisingly powerful because it allows you to store common functions in a centralized location on your drive and then treat your environment like an integrated development environment (IDE). You don't have to worry about where your functions are stored, because you know they're in your local equivalent of `/usr/include`, so no matter where you are when you source them, Bash finds them.
|
||||
|
||||
For instance, you could create a directory called `~/.local/include` as a storage area for common functions and then put this block of code into your `.bashrc` file:
|
||||
|
||||
|
||||
```
|
||||
for i in $HOME/.local/include/*;
|
||||
do source $i
|
||||
done
|
||||
```
|
||||
|
||||
This "imports" any file containing custom functions in `~/.local/include` into your shell environment.
|
||||
|
||||
Bash is the only shell that searches both the current directory and your `PATH` when you use either the `source` or the `.` command.
|
||||
|
||||
### Using source for open source
|
||||
|
||||
Using `source` or `.` to execute files can be a convenient way to affect your environment while keeping your alterations modular. The next time you're thinking of copying and pasting big blocks of code into your `.bashrc` file, consider placing related functions or groups of aliases into dedicated files, and then use `source` to ingest them.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/6/bash-source-command
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bash_command_line.png?itok=k4z94W2U (bash logo on green background)
|
||||
[2]: https://opensource.com/article/17/6/set-path-linux
|
||||
[3]: https://opensource.com/article/20/6/how-write-functions-bash
|
||||
[4]: https://opensource.com/article/19/7/what-posix-richard-stallman-explains
|
@ -0,0 +1,132 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Import functions and variables into Bash with the source command)
|
||||
[#]: via: (https://opensource.com/article/20/6/bash-source-command)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
使用 source 命令将函数和变量导入 Bash
|
||||
======
|
||||
source 就像 Python 的 import 或者 Java 的 include。学习它扩展你的 Bash 能力。
|
||||
![bash logo on green background][1]
|
||||
|
||||
登录 Linux shell 时,你将继承特定的工作环境。对于 shell 而言,_环境_ (environment) 已经为你设置了某些变量,以确保你的命令按预期工作。例如,[PATH][2] 环境变量定义 shell 从哪里查找命令。没有它,几乎所有尝试在 Bash 中执行的所有操作都会因 **command not found** 错误而失败。在执行日常任务时,环境对你几乎是不可见的,但它很重要。
|
||||
|
||||
有多种方法可以影响你的 shell 环境。你可以在配置文件中进行修改,例如 `~/.bashrc` 和 `~/.profile`,你可以在启动时运行服务,还可以创建自己的自定义命令或编写自己的 [Bash 函数][3] 。
|
||||
|
||||
### 通过 source 添加到你的环境
|
||||
|
||||
|
||||
Bash(以及其他一些 shell)有一个称为 `source` 的内置命令。这就是令人困惑的地方:`source` 执行与命令 `.` 相同的功能(是的,那只是一个点),而__不是_与 `Tcl` 命令相同的 `source`(如果你输入 `man source`,可能会在屏幕上显示)。实际上,内置的 `source` 命令根本不在你的 `PATH` 中。这是 Bash 附带的命令,要获取有关它的更多信息,可以输入 `help source`。
|
||||
|
||||
`.` 命令兼容 [POSIX][4]。 但 `source` 命令不是 POSIX 定义的,但可以与 `.` 命令互换。
|
||||
|
||||
根据 Bash `help`,`source` 命令在你当前的 shell 中执行一个文件。 “在你当前的 shell 中”这句很重要,因为它表示它不会启动子 shell。因此,用 `source` 执行的任何操作都发生在内部并影响_当前_环境。
|
||||
|
||||
在探讨 `source` 对环境的影响之前,请在测试文件上尝试 `source` 以确保其按预期执行代码。首先,创建一个简单的 Bash 脚本并将其保存为 `hello.sh`:
|
||||
|
||||
|
||||
```
|
||||
#!/usr/bin/env bash
|
||||
echo "hello world"
|
||||
```
|
||||
|
||||
使用 `source`,即使不设置可执行也可以运行此脚本:
|
||||
|
||||
|
||||
```
|
||||
$ source hello.sh
|
||||
hello world
|
||||
```
|
||||
|
||||
你也可以使用内置的 `.` 命令获得相同的结果:
|
||||
|
||||
|
||||
```
|
||||
$ . hello.sh
|
||||
hello world
|
||||
```
|
||||
|
||||
`source` 和 `.` 命令成功执行测试文件的内容。
|
||||
|
||||
### 设置变量和导入函数
|
||||
|
||||
你可以使用 `source` 将文件“导入”到 shell 环境中,就像你可以在 C 或 C++ 中使用 `include` 关键字引用一个库,或者在 Python 中使用 `import` 关键字引入一个模块一样。这是 `source` 的最常见用法之一,它也是 `.bashrc` 中的一个默认包含项,通过 `source` 导入 `.bash_aliases` 来将任何你自定义的别名在登录时导入。
|
||||
|
||||
这是导入 Bash 函数的示例。首先,在名为 `myfunctions` 的文件中创建一个函数。它将打印你的公共 IP 地址和本地 IP 地址:
|
||||
|
||||
|
||||
```
|
||||
function myip() {
|
||||
curl <http://icanhazip.com>
|
||||
|
||||
ip addr | grep inet$IP | \
|
||||
cut -d"/" -f 1 | \
|
||||
grep -v 127\\.0 | \
|
||||
grep -v \:\:1 | \
|
||||
awk '{$1=$1};1'
|
||||
}
|
||||
```
|
||||
|
||||
将函数导入你的 shell:
|
||||
|
||||
|
||||
```
|
||||
`$ source myfunctions`
|
||||
```
|
||||
|
||||
测试新函数:
|
||||
|
||||
|
||||
```
|
||||
$ myip
|
||||
93.184.216.34
|
||||
inet 192.168.0.23
|
||||
inet6 fbd4:e85f:49c:2121:ce12:ef79:0e77:59d1
|
||||
inet 10.8.42.38
|
||||
```
|
||||
|
||||
### source 搜索
|
||||
|
||||
当你在Bash中使用 `source` 时,它将在当前目录中搜索你引用的文件。但并非所有 shell 都这样,因此,如果你不使用 Bash,请查看文档。
|
||||
|
||||
如果 Bash 找不到要执行的文件,它将搜索你的 `PATH`。同样,这并不是所有 shell 的默认设置,因此,如果你不使用 Bash,请查看文档。
|
||||
|
||||
这些都是 Bash 中不错的便利功能。这种出奇地强大,因为它允许你将常用函数保存在磁盘上的一个集中位置,然后将你的环境视为集成开发环境 (IDE)。你不必担心函数的存储位置,因为你知道它们在本地等同于在 `/usr/include` 下,因此无论你在哪,当你 source 过它们,Bash 都可以找到它们。
|
||||
|
||||
例如,你可以创建一个名为 `~/.local/include` 的目录作为常见函数存储区,然后将此代码块放入 .bashrc 文件中:
|
||||
|
||||
|
||||
```
|
||||
for i in $HOME/.local/include/*;
|
||||
do source $i
|
||||
done
|
||||
```
|
||||
|
||||
这会将 `~/.local/include` 中所有包含自定义函数的文件“导入”到 shell 环境中。
|
||||
|
||||
当你使用 `source` 或 `.` 命令时,Bash 是唯一搜索当前目录和 `PATH` 的 shell。
|
||||
|
||||
### 将 source 用于开源
|
||||
|
||||
使用 `source` 或 `.` 执行文件是影响环境同时保持变更模块化的一种便捷方法。在下次考虑将大量代码复制并粘贴到 .bashrc 文件中时,请考虑将相关函数或别名组放入专用文件中,然后使用 source 获取它们。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/20/6/bash-source-command
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/bash_command_line.png?itok=k4z94W2U (bash logo on green background)
|
||||
[2]: https://opensource.com/article/17/6/set-path-linux
|
||||
[3]: https://opensource.com/article/20/6/how-write-functions-bash
|
||||
[4]: https://opensource.com/article/19/7/what-posix-richard-stallman-explains
|
Loading…
Reference in New Issue
Block a user