开发者

How to make a shell script global?

I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).

For example, if I save a .sh file in the /Users/username/ directory and make it executa开发者_开发百科ble, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.

Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks


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


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)


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.


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


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.


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.


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


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 :.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜