In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to use the source command to import functions and variables into Bash, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Source is like Python's import or Java's include. Learn it to expand your Bash capabilities.
When you log in to Linux shell, you will inherit a specific work environment. For shell, "environment environment" means that some variables have been set for you to ensure that your commands work as expected. For example, the PATH environment variable defines where shell looks for commands. Without it, almost everything you try to perform in Bash will fail because the command does not find an error. The environment is almost invisible to you when performing daily tasks, but it is important.
There are several ways to affect your shell environment. You can make changes in the configuration file, such as ~ / .bashrc and ~ / .profile, you can run the service at startup, you can create your own custom commands or write your own Bash functions.
Add to your environment through source
Bash (and some other shell) has a built-in command called source. That's what's confusing: source execution and command. The same function (yes, that's just a dot), but not the same source as the Tcl command (if you type man source, maybe it's displayed on the screen). In fact, the built-in source command is not in your PATH at all. This is the command that comes with Bash. For more information about it, type help source.
. Command is compatible with POSIX. However, the source command is not defined by POSIX, but can be associated with. Commands are used interchangeably.
Execute a file in your current shell according to the Bash help,source command. The phrase "in your current shell" is important because it indicates that it does not promote shell. Therefore, anything done with source takes place internally and affects the current environment.
Before exploring the impact of source on the environment, import a test file with the source command to ensure that the code executes as expected. First, create a simple Bash script and save it as hello.sh:
#! / usr/bin/env bashecho "hello world"
With source, the script can be run even if it is not set to be executable:
$source hello.shhello world
You can also use the built-in. Command to get the same result:
$. Hello.shhello world
Source and. The command successfully executed the contents of the test file.
Set variables and import functions
You can use source to "import" files into the shell environment, just as you can use the include keyword to reference a library in C or C++, or use the import keyword to introduce a module in Python. This is one of the most common uses of source, and it is also a default inclusion method in .bashrc, which imports .bash _ aliases through source to import any aliases you customize into your environment when you log in.
This is an example of importing a Bash function. First, create a function in a file called myfunctions. It will print your public IP address and local IP address:
Function myip () {curl ip addr | grep inet$IP |\ cut-d "/"-f 1 |\ grep-v 127\\ .0 |\ grep-v\:\: 1 |\ awk'{$1room1}; 1'}
Import this function into your shell:
$source myfunctions
Test the new function:
Search for $myip93.184.216.34inet 192.168.0.23inet6 fbd4:e85f:49c:2121:ce12:ef79:0e77:59d1inet 10.8.42.38source
When you use source in Bash, it will search the current directory for the files you reference. But not all shell are like this, so if you don't use Bash, please check the documentation.
If Bash cannot find the file to execute, it will search for your PATH. Again, this is not the default setting for all shell, so if you do not use Bash, please check the documentation.
These are all good convenience features in Bash. This is surprisingly powerful because it allows you to store common functions in a centralized location on disk and then treat your environment as an integrated development environment (IDE). You don't have to worry about where functions are stored, because you know that they are equivalent to / usr/include in your local location, so wherever you are, Bash can find them when you import them.
For example, you can create a directory called ~ /. Local/include as a common function store, and then put this code block in the .bashrc file:
For i in $HOME/.local/include/*; do source $idone
This "imports" all files in ~ / .local/include that contain custom functions into the shell environment.
When you use source or. Command, Bash is the only shell that searches the current directory and PATH.
Using source for open source
Use source or. To execute the file is a convenient way to affect the environment while keeping the change modular. The next time you consider copying and pasting a large amount of code into a .bashrc file, consider putting related functions or alias groups in a dedicated file, and then import them using source.
These are all the contents of the article "how to import functions and variables into Bash using source commands". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.