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.

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:


Now on your home directory find a file called .bashrc. If you are using a different shell (like zsh) the file might be different. For example if you are using zsh you should look for .zshrc.

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

Popular posts from this blog

DTOs conversion with reflection example - applied on Thymeleaf (or how to get read of Pojo's conversion boilerplate)

How to create a Cassandra container for testing with Keyspace and the latest schema with a single script call