如何使 shell 脚本全局?

我使用的是 Mac 的 OS 10.6,我正在尝试学习一些关于 shell 脚本的知识。我知道如何保存 shell 脚本并使其可执行,但是我想知道我能做什么,或者我可以在哪里保存文件以使其全局化(也就是说,无论我在哪个文件夹中都可以访问)。

例如,如果我保存一个。在/Users/username/目录中执行 sh 文件,并使其成为可执行文件,我只能在该特定目录中执行该脚本。例如,如果导航到/Users/username/Downloads,则无法执行该脚本。

此外,任何关于学习更多 shell 脚本的资源建议都会很有帮助

108428 次浏览

You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).

You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.

You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.

/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default

Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.

If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.

On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"

Now execute the script by function call in the shell.

There are two ways to do it -

  1. Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
  2. Create a folder in your home directory called bin. (For your personal scripts)
    • cd ~ (Takes you to your home directory)
    • mkdir bin (create a bin folder)
    • vim .bash_profile (to set path environment variable)
    • export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
    • Now you can just type the name of your script and run it from anywhere you want.

** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)

In mac operating system

  • Open bash ~/.bashrc file.
  • add path of your script in your bashrc file , using export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
  • Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
  • open new terminal window Now whenever you will open your terminal your script will be loaded

This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.

(I use Arch Linux Manjaro so I use .bashrc located in my home directory)

The code to be placed in your .bashrc file:

# Simple bashrc method to launch anything in terminal from any directory


YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}

As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)

Hope I've helped someone!

Sincerely,

AnonymousX

from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)