Run Bash Script Using Name [Duplicate]
I'm New to Bash Scripting and Made One for My Vpn Called Myvpn. Sh Instead of Doing. /Myvpn. Sh I Want to Just Write Myvpn in Terminal and It Will Run It from...
I'm new to bash scripting and made one for my VPN called myvpn.sh
Instead of doing ./myvpn.sh I want to just write myvpn in terminal and it will run it from any directory. Tried searching Google for it but with no luck (probably googling the wrong thing).
Is this possible to do by an amateur or does it requite any advanced knowledge?
4 Answers
You can make alias of the path to your script. Firstly, make your command executable by using chmod:
chmod u+x myvpn.sh
where u+x gives only the user(you) the permission to execute the script.
Then make an alias in your .bashrc to make it persistent:
alias myvpn='/path/to/your/script'
Now you can simply type myvpn from anywhere to execute the script.
Hello and welcome on Ask Ubuntu.
Lucky you, your question does not require advanced knowledge at all!
Must Read
1) Locate folders where executable are located
To be executed with only its name, a binary file must be in one of the directories present in the PATH environment variable.
The default directory for user executable files on Ubuntu is $HOME/bin. If it doesn't exist, you can create it manually and it will be automatically added in your PATH variable. $HOME is an environment variable that holds the path to your user's home directory, meaning that this "bin" folder is to be created in the same folder as "Documents", "Pictures", "Music"...*
To know what you have in your PATH variable, the simplest way is to open a terminal and type echo $PATH, as suggested by vanadium.
You can also view all your environment variables with env.
You should get something like (plus other variables if you used env):
PATH=/home/datam/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
The folders are separated by ":" sign.
Every time you type a command in terminal, your OS looks into these directories until it finds a corresponding executable file!
Here you can see that there is a folder in your home directory i.e. accessible without sudo: $HOME/.local/bin
If you have added a bin folder manually, then you'll see it in your path (after logging out and logging back in again).
That's where your file should be!