3

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?

2

4 Answers 4

5

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.

3

Hello and welcome on Ask Ubuntu.
Lucky you, your question does not require advanced knowledge at all!

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!

2) Make your file executable

So ensure your file is executable by your user i.e. has x permission for "u" (graphically with right-click -> properties -> permissions depending on your distribution, in command line with ls -l from the folder where your file stands). Edit your script to add #!/bin/bash on its first line if not already done.

3) Move the file into the right folder

Now, move or copy the file into $HOME/bin

Job done :) you can now execute it from terminal by just typing file name!

4) Rename the file (optional)

If the filename of your script is "myvpn.sh", you will write "myvpn.sh" in terminal to invoke it. So if you want to call the script with just "myvpn", you have to rename your file.

6
  • 1
    Why env? If you want PATH, just run echo $PATH. And why ~/.local/bin? The default directory for user executable files on Ubuntu systems is ~/bin. The ~/.local directory is used for configuration normally and is unlikely to be in the PATH of a new user.
    – terdon
    Jan 8, 2020 at 10:35
  • "Why env? If you want PATH, just run echo $PATH." -> Because other users already gave this option so I wanted to give an alternative. "And why ~/.local/bin? The default directory for user executable files on ubuntu systems is ~/bin. The ~/.local directory is used for configuration normally and is unlikely to be in the PATH of a new user." -> I have just tried it on a Lubuntu virtual machine, I have no ~/bin but I have ~/.local/bin... "And there is no reason at all to remove the extension." -> If the script doesn't start with #!/bin/bash then it doesn't work. Thanks for your remarks!
    – FloT
    Jan 8, 2020 at 10:37
  • 1
    Yes, ~/bin doesn't exist by default, but the default .bashrc is configured to include it in the PATH. So if you create ~/bin, it will be in your PATH automatically. It's generally not a good idea to use hidden directories for this. I mean, it will work, it's just strange to use ~/.local/bin. As for the extension, I deleted that from my comment, sorry. I hadn't seen that the OP was asking for that. But your comment here is wrong, the extension is completely irrelevant. It doesn't affect anything at all. If the script doesn't have #!/bin/bash, the extension won't change anything.
    – terdon
    Jan 8, 2020 at 10:41
  • "~/bin doesn't exist by default, but the default .bashrc is configured to include it in the PATH. So if you create ~/bin, it will be in your PATH automatically." -> I didn't know, awesome, thanks! I have tried to edit my answer as per your comments. Regarding to extension, I have tried again and it works without the header if I call it with its full name... It couldn't make it run without calling its exact name (extension included) with our without the header. Maybe a Lubuntu specific behavior?
    – FloT
    Jan 8, 2020 at 11:20
  • 1
    Ah, no you always need the exact name. So if it's foo, call it as foo and if it's foo.whatever call it as foo.whatever. but that is just because the name needs to be correct. The extension doesn't make any difference to anything. With very few exceptions (e.g. gzip requires the .gz extension), extensions are only useful to us humans as an easy way of knowing what a file is. The OS doesn't use them on Linux systems.
    – terdon
    Jan 8, 2020 at 11:49
2

You could move your script to /usr/local/bin and make it executable with chmod 0750 myvpn.sh. This should allow you to run your script by just typing it's name. Just in case, make sure your script starts with #!/bin/bash.

1

In order to be able to run the script simply by typing its name, the script should reside in a folder that is listed in your search path. The command echo $PATH will list your current search paths. When you type the name of an executable, the system searches the folders listed in the PATH variable in order to find an executable with the name you typed.

If you want your script to be available for all users, you will need to place it in one of the folders under the system folders. For that, you need administrator privileges.

If you are the only user that needs to use the script, then create a bin folder under your home folder (i.e. the folder that also contains Documents, Pictures etc). Then log out and back in. Ubuntu is configured to automatically include your local bin folder in the PATH. After this, you will be able to run any executable that you place in your local bin folder by typing the file name.

A detail: if you want to type myvpn to run your custom script, then you also will need to name it like that: thus, rename myvpn.sh to myvpn. Also do not forget to make the script executable. After you moved the script to your local bin folder, the following command will make it executable:

chmod +x ~/bin/myscript

You can also use files (nautilus) to make the script executable: right-click the script, select "Properties" and go to the "Permissions" tab.

Not the answer you're looking for? Browse other questions tagged .