In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to make linux scripts can be executed anywhere". In daily operations, I believe many people have doubts about how to make linux scripts executable anywhere. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "how to make linux scripts executable anywhere". Next, please follow the editor to study!
How do they do it?
That's because the path to the program corresponding to these commands is added to the Path environment variable. So if we add our own script path to this variable, we can also achieve the effect of executing our script anywhere.
Let's introduce three ways to achieve this.
Method 1: add the path of the script to the `.bashrc` file
We know that after Bash is running, the .bashrc file is loaded first. So, we can add the script path to the .bashrc file, and then we can achieve our goal.
Suppose I now put the script in the / home/alvin/scripts directory, the name of the script is hello.sh, and the result of running is to print a hello world.
Then, let's add the following code to the end of .bashrc:
Export PATH= "/ home/alvin/scripts:$PATH"
Finally, use the source command to make our changes take effect:
$source ~ / .bashrc
Now, we can use our own scripts anywhere.
Method 2: add the path of the script to the `.profile` file
The .profile file is an environment variable configuration file that users load when logging in to the system to configure environment variables. So we can add our path to this file so that the script can be used anywhere.
We can add the following code at the end of the .proflie file:
Export PATH= "$PATH:$HOME/scripts"
Then, we need to log out of the terminal and log in again for the changes to take effect.
If you use Ubuntu and its derivative distributions, you need to modify the .pam _ environment file instead of the .profile file. Similarly, add a line of code at the end of the file:
PATH DEFAULT=$ {PATH}: / home/@ {PAM_USER} / scripts
Finally, log in again to enable modification.
At this point, you can execute your script anywhere.
Method 3: add the path where the script is located to the environment variable
This method should be able to think of, relatively simple and rough, directly change the environment variable file. The file is typically located in the / etc directory and the file name is environment.
Sudo vim / etc/environment
We found the following sentence in the file:
PATH= "/ usr/local/sbin:/usr/local/bin:/usr/sbin"
All we have to do is add our script path after this sentence.
PATH= "/ usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/alvin/scripts"
As in method 2, we need to log the user out and log in again, and then we can achieve our goal.
At this point, the study on "how to make linux scripts executable anywhere" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.