How do I make a command in linux using a bash script? - linux

I am trying to make a command for the terminal. I have a bash script prepared (la.sh) and I want to just be able to type la to run it. How am I able to get the code so that I can just type la?
I have tried putting it in the /bin folder however had no luck.
What can I do to fix this?
I am using the latest version of Manjaro Gnome.
Thanks a lot!!!
BTW, the script was literally just ls.
It was just a practice script.

Lets consider that your script is stored under /some/path/la.sh. In my opinion, you have several solutions to accomplish your goal:
Option 1:
Add the script to your user's path so you can directly call it.
echo "export PATH=$PATH:/some/path/" >> ~/.bashrc
Then you will be able to use in your terminal:
$ la.sh
Using this option you can call la.sh with any parameters if needed. If the requirement is to call simply la you can also rename the script or create a softlink:
mv /some/path/la.sh /some/path/la
or
ln -s /some/path/la.sh /some/path/la
Option 2:
Create an alias for the script.
echo "alias la='/some/path/la.sh'" >> ~.bashrc
Then you will be able to use in your terminal:
$ la
However, using this option you will not be able to pass arguments to your script (executing something similar to la param1 param2) unless you define a more complex alias (an alias using a function in the .bashrc, but I think this is out of the scope of the question).
IMPORTANT NOTE: Remember to reload the environment in your terminal (source .bashrc) or to close and open again the terminal EVERY TIME you make modifications to the .bashrc file. Otherwise, you will not be able to see any changes.

The file la.sh must be placed in your path. Then you can create an alias for it.
alias la="la.sh"

If you want to have a command be available under two different names (la.sh and la in your case), I recommend against using an alias: An alias defined in your .bashrc is only available in an interactive bash; If you run, say, a non-bash interactive shell, or writing a bash script, you can't use it.
The IMO most general way is to create a link. Since you said that you have already placed la.sh into bin, you can create the link in the same directory, i.e.
ln /bin/la /bin/la.sh # creates a hard link
or
ln -s /bin/la /bin/la.sh # creates a symbolic link
In your case, either one is fine. If you want to find out more about the differences between hard and symbolic link, look for instance here.

It worked with a mixture of everybody's answers.
All I had to do was go into the directory that la.sh was in.
Rename it to just la as a text file.
Run chmod 777 la to turn it into executable to anybody.
Add it to my path by using the command export PATH=$PATH:~/Directory/It/Was/In/
Thank you to all who contributed.

Related

creating a command to go to a specific shortcut

So basically, I just installed ubuntu on bash on my windows PC and every time I have to go to desktop I have to type cd /mnt/c/Users/Name/Desktop. is there a way that I can make a script or command so everytime i type in "desktop" it changes my directory to desktop? I've never done Linux/bash scripting and therefore I have no clue. I just use it for the g++ compiler.
There are many ways to do this:
Create an executable program that runs the command cd /mnt/c/Users/Name/Desktop (this is iain's answer, and a perfectly fine one; the only issue might be that you'll need to either enter the full path for that program every time you run it, or you'll need to put it into a directory that is already included in your $PATH environment variable.)
Create an environment variable that contains the path you'd like, then cd to it cd $DESKTOP.
export DESKTOP="/mnt/c/Users/Name/Desktop"
cd $DESKTOP
You'll likely want to put the creation of this environment variable into your .bashrc or .profile, so it gets created each time you login.
echo 'export DESKTOP="/mnt/c/Users/Name/Desktop"' >> ~/.bashrc
(Note: the above adds it to the end of the .bashrc file. Once you learn more about bash and .bashrc you will probably want to move it to another location in the file.)
Create an alias that does the same thing.
alias mydesk='cd /mnt/c/Users/Name/Desktop'
Again, you'll likely want to add the creation of this alias to your .bashrc or .profile file, so it gets created each time you login:
echo "alias mydesk='cd /mnt/c/Users/Name/Desktop'" >> ~/.bashrc
(Note: the above adds it to the end of the .bashrc file. Once you learn more about bash and .bashrc you will probably want to move it to another location in the file or even a different file completely.)
Assuming that "/mnt/c/Users/Name/" is your home directory, you can just use the shortcut for that, then append "Desktop" to it:
cd ~/Desktop
or
cd $HOME/Desktop
You can give this a try ...
make a file called desktop
Put this into it:
#!/bin/bash
cd /mnt/c/Users/Name/Desktop
close the file and then make it executable.
$ chmod +x desktop
Now by typing . desktop (note the dot) you should be taken to your desktop.
You may also be able to add the script to your path, so that it will run from anywhere in the bash environment; Depending upon how like ubuntu your environment is.

How to make a fish script executable in any directory?

I made the switch to Ubuntu a couple of months ago and still do not have very much experience with unix and fish aside from the basic/often used commands. For a C class that I am taking I constantly need to SSH onto a remote linux machine run by my university called Zeus. The command to ssh onto it is a bit lengthy, and I would like to be able to make the process faster with a fish script.
I wrote a file called "zeus.fish" that essentially just calls my ssh command, among a few other things. Currently I can only run it by calling "fish zeus.fish" within the directory of the file. I would like to be able to run the script from within any directory, just like any other command. IE just typing "zeus" would attempt to log me into the remote machine. I'm assuming that I have to do something with PATH. How can I do this? Thanks!
You can make an executable and put it somewhere in $PATH. However, the simplest thing is to make a zeus function. A fish function is like a function in other languages: some named block of code.
Run this: function zeus; your_long_command_goes_here ; end
Run funcsave zeus to save it permanently
Now zeus will run that command.
What this does is put a file zeus.fish in ~/.config/fish/functions/. You can also do that manually of course.
I think this should work.
Add this to the first line of the script:
#!/bin/fish
That way it knows to execute your script with fish. Otherwise you would have to use ./scriptname or fish scriptname, to indicate you wanted fish to execute it.
Remove the .fish ending, if you want to call it just by "zeus".
Add the file to your path. In your .fishrc, write:
export PATH=XXX:$PATH
Okay, so I've never used fish. Apperently they don't use .fishrc, put it into your equivalent of .bashrc. Which is ~/.config/fish/config.fish as far as I can see. Actually the line you have to write is
set -x PATH $PATH XXX
With XXX being the path to your file, for example copy it into a folder named bin in your home directory. Then it would be
set -x PATH $PATH $HOME/bin
Last but not least, make the file executeable by writing:
chmod +x FILENAME
It seems like an alias would be a simpler solution for you however.
In your fish equivalent of .bashrc, you can do:
function zeus
ssh username#server
end
This seems to be the fish equivalent of an alias. If it is just 1 line that is long, you could do it like this. Having the line inside the function. Now you could just call "zeus" from anywhere aswell.
ridiculous_fish's answer is (obviously) entirely correct, but actually complicating it a bit in my opinion.
funced zeus; and funcsave zeus
will do the same thing in one go, launching an (empty) interactive function definition prompt in the first command, instantly saving it when editing finishes. If there is already a function of that name it will show up in the editor, so it's easy to continuously tweak your functions without having to dive into (or even consider the existence of) your fish config dir.
One of the very first functions I made when picking up the shell was "func", that makes exactly those two calls (since I only rarely edit a function without the intention of saving my changes)
For simple ssh stuff and other oneliners that don't take any input or where the input ($argv) is sure to just be appended at the end of the line,
alias zeus='ssh ...'; and funcsave zeus
will do the same. Any further editing will have to go through funced though.

Creating permanent executable aliases

I have MySQL installed (MAMP, Mac OS X) but need to call it by the full path each time I access it from the shell. I created an alias: alias mysql='/Applications/MAMP/Library/Bin/mysql, but this only lasts as long as my terminal/Bash session.
What is an effective way for establishing permanent aliases that will work across users? (I need to be able to execute commands from PHP). Should I set up aliases in the Bash start up script (how is that done?), or is it better to edit the sudoers file? (Could use an example of that as well..)
Thanks--
EDIT- Based on answer:
I just tried creating a ~/.bashrc and wrote the following:
alias mysql='/Applications/MAMP/Library/bin/mysql'
But this doesn't seem to have any effect. Is there a special syntax for this file?
Add the command to your ~/.bashrc file.
To make it available to all users, add it to /etc/profile.
Different shell uses different dot file to store aliases.
For mac, the bash shell uses .bash_profile or .profile
For ubuntu, the bash shell uses.bashrc
If you are using zsh shell and ohmyzsh plugin, the dot file is .zshrc
Traditionally, to add a permanent alias, you need to open the dot file and write alias manually like:
alias hello="echo helloworld"
And remember to source the dot file to have it take effect. To source the dot file on ubuntu's bash, type source .bashrc To make the alias available to all users, write to /etc/profile instead of the dot file. Remember to type source /etc/profile for the new alias to take effect.
If you simply want a temporary alias, you do not need to write to dot file. Simply type that same command (alias hello="echo helloworld) on the terminal.
Note that a temporary alias created through the alias command will disappear once the shell is closed.
If you are looking for a single command to generate aliases without open the text editor, read on.
If you have ruby installed on ubuntu, you can create permanent alias with a single command using aka.
gem install aka2
For example:
aka generate hello="echo helloworld" #will generate a alias hello="echo helloworld"
aka destroy hello #will destroy the alias hello
aka edit hello #will prompt you to edit the alias.
With aka, there is no need to write to the dot file with a text editor. And no need to source the dot file too.
You're going about this the wrong way.
Either add /Applications/MAMP/Library/bin/ to your path, or create a script to invoke MySQL and place it in a bin directory which is already in your path.
On a mac the .bashrc file does not get sourced unless you put
source ~/.bashrc in the /etc/profile or /etc/bashrc.
Just thought I would mention that.

Creating a shorter version of a bash command

i am novice to the Linux shell and had to recently start using it for work...i have now got used to the basic commands in bash to find my way around...however there are a lot of commands i find myself typing over and over again and its kind of a hassle to type them every time...so can anyone tell me how can i shorten the command syntax for ones i use frequently.
A very simple example, i use the ls -lh command often, though this is quite short but im just giving an example. Can I have something (a shell script may be) so that I can run it by typing just say lh.
I want to do it for more complex commands.
alias lh='ls -lh'
If you want to make this persistent across sessions, put it in your .bashrc file. Don't forget to run source .bashrc afterwards to make bash aware of the changes.
If you want to pass variables, an alias just isn't enough. You can make a function. As an example, consider the command lsall to list everything in a given directory (note this is just an example and thus very error prone):
function lsall
{
ls $1/*
}
$Ngets replaced with the Nth argument.
You would place the following alias in your .bashrc file:
alias lh='ls -lh'
Now lh is shorthand for ls -lh.
For more complicated tasks you could use a bash function. For example, on one of my machines I have a function which causes 'ls' to run after every successful 'cd':
cdls() {
builtin cd "$*" && ls
}
alias cd='cdls'
you can define aliases. For longer commands, use a function, put it into a library file and source it whenever you want to use your functions.
Just for the sake of completeness, since you want to learn bash: you could also write a function
lh() {
ls -lh "$#"
}
although I would never write that when a simple alias would do ;-)
;) Heh, I remember one problem when I was starting out on Linux, which is that I would ask questions like these, and people would diligently answer them, but no one would explain how to make such changes permanent, and so I found myself typing in a bunch of commands every time I opened a terminal.
So, even though others have accurately answered this question... if you want to make the change permanent, put the alias-line into your ~/.profile or ~/.bashrc file (~ = your home directory). It depends a bit on your distribution on which is run when, but I always try adding my aliases to ~/.profile first and if that doesn't work, then ~/.bashrc. One of them should work for sure.

How can I define a bash alias as a sequence of multiple commands? [duplicate]

This question already has answers here:
Multiple commands in an alias for bash
(10 answers)
Closed 4 years ago.
I know how to configure aliases in bash, but is there a way to configure an alias for a sequence of commands?
I.e say I want one command to change to a particular directory, then run another command.
In addition, is there a way to setup a command that runs "sudo mycommand", then enters the password? In the MS-DOS days I'd be looking for a .bat file but I'm unsure of the linux (or in this case Mac OSX) equivalent.
For chaining a sequence of commands, try this:
alias x='command1;command2;command3;'
Or you can do this:
alias x='command1 && command2 && command3'
The && makes it only execute subsequent commands if the previous returns successful.
Also for entering passwords interactively, or interfacing with other programs like that, check out expect. (http://expect.nist.gov/)
You mention BAT files so perhaps what you want is to write a shell script. If so then just enter the commands you want line-by-line into a file like so:
command1
command2
and ask bash to execute the file:
bash myscript.sh
If you want to be able to invoke the script directly without typing "bash" then add the following line as the first line of the file:
#! /bin/bash
command1
command2
Then mark the file as executable:
chmod 755 myscript.sh
Now you can run it just like any other executable:
./myscript.sh
Note that unix doesn't really care about file extensions. You can simply name the file "myscript" without the ".sh" extension if you like. It's that special first line that is important. For example, if you want to write your script in the Perl programming language instead of bash the first line would be:
#! /usr/bin/perl
That first line tells your shell what interpreter to invoke to execute your script.
Also, if you now copy your script into one of the directories listed in the $PATH environment variable then you can call it from anywhere by simply typing its file name:
myscript.sh
Even tab-completion works. Which is why I usually include a ~/bin directory in my $PATH so that I can easily install personal scripts. And best of all, once you have a bunch of personal scripts that you are used to having you can easily port them to any new unix machine by copying your personal ~/bin directory.
it's probably easier to define functions for these types of things than aliases, keeps things more readable if you want to do more than a command or two:
In your .bashrc
perform_my_command() {
pushd /some_dir
my_command "$#"
popd
}
Then on the command line you can simply do:
perform_my_command my_parameter my_other_parameter "my quoted parameter"
You could do anything you like in a function, call other functions, etc.
You may want to have a look at the Advanced Bash Scripting Guide for in depth knowledge.
For the alias you can use this:
alias sequence='command1 -args; command2 -args;'
or if the second command must be executed only if the first one succeeds use:
alias sequence='command1 -args && command2 -args'
Your best bet is probably a shell function instead of an alias if the logic becomes more complex or if you need to add parameters (though bash supports aliases parameters).
This function can be defined in your .profile or .bashrc. The subshell is to avoid changing your working directory.
function myfunc {
( cd /tmp; command )
}
then from your command prompt
$ myfunc
For your second question you can just add your command to /etc/sudoers (if you are completely sure of what you are doing)
myuser ALL = NOPASSWD: \
/bin/mycommand
Apropos multiple commands in a single alias, you can use one of the logical operators to combine them. Here's one to switch to a directory and do an ls on it
alias x="cd /tmp && ls -al"
Another option is to use a shell function. These are sh/zsh/bash commands. I don't know enough of other shells to be sure if they work.
As for the sudo thing, if you want that (although I don't think it's a good idea), the right way to go is to alter the /etc/sudoers file to get what you want.
You can embed the function declaration followed by the function in the alias itself, like so:
alias my_alias='f() { do_stuff_with "$#" (arguments)" ...; }; f'
The benefit of this approach over just declaring the function by itself is that you can have a peace of mind that your function is not going to be overriden by some other script you're sourcing (or using .), which might use its own helper under the same name.
E.g., Suppose you have a script init-my-workspace.sh that you're calling like . init-my-workspace.sh or source init-my-workspace.sh whose purpose is to set or export a bunch of environment variables (e.g., JAVA_HOME, PYTHON_PATH etc.). If you happen to have a function my_alias inside there, as well, then you're out of luck as the latest function declaration withing the same shell instance wins.
Conversely, aliases have separate namespace and even in case of name clash, they are looked up first. Therefore, for customization relevant to interactive usage, you should only ever use aliases.
Finally, note that the practice of putting all the aliases in the same place (e.g., ~/.bash_aliases) enables you to easily spot any name clashes.
you can also write a shell function; example for " cd " and "ls " combo here

Resources