Make your bash script runnable from every directory on the terminal
Have you ever wanted to create a utility like ls for example and make it available from everywhere in the linux's/mac's terminal?
There two ways to do that.
a.move your script to /bin
sudo mv hello /bin/hello
b.make your script executable
sudo chmod +x /bin/hello
you are ready to ramble!
create a file with different functions and aliases like the example bellow. Lets assume that the file name is .myfunc
the file might look like:
There two ways to do that.
Way 1
(lets assume that my script is in a file with the name hello.)a.move your script to /bin
sudo mv hello /bin/hello
b.make your script executable
sudo chmod +x /bin/hello
you are ready to ramble!
Way 2
create a file with different functions and aliases like the example bellow. Lets assume that the file name is .myfunc
the file might look like:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hello { | |
echo "hello world!" | |
} | |
function again { | |
echo "hello again" | |
} | |
alias hi='echo hi' |
Mind that the file names starting with dot (.) in POSIX systems (like linux and macOs) are hidden files.
when you find the file .bashrc, edit it with VI or the editor of your choice and add somewhere (it does not matter where) the line
source {path to your .myfunc file}
If .myfunc is stored on the path ~/myscripts/.myfunc then the line on .bashrc should look something like the following
source ~/myscripts/.myfunc
You are almost done. Now you either have to restart the terminal session or source the .bashrc
source .bashrc
Now you are ready to call your supper useful functions hello, again and hi from wherever in the terminal.
try it ;)
Comments
Post a Comment