I have a script script.sh but I am very lazy if when running must open through the folder where the file and run it in the way ./script.sh,
How do I get my script to use keywords?
I have a script.sh file
if I write it in the command
"testscript" will then open the script.sh file
help please
This one is pretty easy, but I'm going to assume that you don't already know about aliases.
Aliases are ways to turn your own customized commands into actual functioning commands. They come in pretty handy.
Open your home directory and create a plain text file called
.bash_aliases
. Go ahead and save it - but leave it open in your editor.Now, think of a word or phrase you'd like to use to execute your script. In my case, I used
test
.In my Documents directory, I made a file called
test.sh
and inside it contains:NOTE You'll want to not forget to make your script executable. You can do that with
chmod +x
or in the GUI with a right click/properties menu option.Now, back in your
.bash_aliases
file, you'll want to add it as a command. In my case, it looks like this:NOTE In all those cases, the "." and capitalization are essential. It's Case Sensitive.
Now, you need to tell your system to look for (and reload any new) aliases. This is done in the terminal with the following:
After all that, I can do this:
As you can see, you're telling it where the aliases are and you're reloading them. Any time you add new aliases, you'll need to run that to make them usable.
EDIT: While the above method will work, it will only work when the terminal is opened in your
/home/user/
directory. That's fine and that's the default place most terminals open, but not all terminal work is done in that directory.The answer is this:
Now, no matter what directory you're in, you can type
test.sh
and it will work. This doesn't need elevated permissions and the script can then only operate with your user permissions or whatever additional permissions you grant at the time of execution.If I had to pick, I'd go with this method - the one where you use the user's home directory variable of
$HOME
(again, case sensitive) as a generic user can set it up, configure it, use it, etc... It doesn't require root access or any extra permissions.As mentioned elsewhere in this answer, there are a variety of ways to accomplish this. Pick the one that best suits your needs and requires the least permissions.
Method #2:
I got bored and decided to add another way.
After you make your script executable, you can move your script to
usr/bin
and just make it a command. In my case, I used a GUI.In the terminal, I entered:
Then, I just copied the test.sh to
usr/bin
- and it retained the executable permissions I'd applied before.At this point, I could just use
test.sh
and it would work.I wasn't happy with that, so I renamed it to just plain
test1
and removed the .sh from the file name. Linux doesn't really care too much about the extension names. I couldn't name ittest
as there was already a test in theusr/bin
directory. So,test1
is what I named it. Obviously, you can name it anything you want, so long as it doesn't conflict with other file names.Now, from the terminal, it works just like this:
As you can see, there are a variety of ways to do this. I'm most fond of the alias method, as it doesn't require any elevated permissions and I'm a big fan of the 'least permissions' method.