Creating permanent executable aliases - linux

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.

Related

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

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.

Environment variable PATH on linux

Hi i am currently trying to set keywords for my terminal to launch some software without having to type the whole path.
For exemple:
firefox
#instead of
/home/debian/firefox/firefox
I always do this kind of thing on windows by setting path in the environment variable manager.
After i read this post PATH environment variable in linux , i added this line to the etc/environment file:
export firefox=/home/debian/firefox/firefox
#I also tried this:
export PATH=$PATH:/home/debian/firefox
It doesn't work, can someone explains me how to do this?
I would setup a new alias in my .bashrc or .profile, which should be located under your home directory. Add the following to the end of the file:
alias firefox="/home/debian/firefox/firefox"
Save the file and reload it using:
source ~/.bashrc
Since you added the alias to your .bashrc this alias will be created everytime you open a new instance of a shell.
You could use nohup to keep the command running after the shell session ends:
alias firefox="nohup /home/debian/firefox/firefox &"
Notice the trailing & character, which will run the command in the background so you can keep using your terminal.
You can also make an alias in your .bashrc file.
$ vim ~/.bashrc
It will open your .bashrc in read mode. Get in write mode by pressing i. You can create alias at anywhere in the file or below already created alias list.
alias firefox='/home/debian/firefox/firefox'
press Esc and then :wq
This will create your alias, save and exit the file. Now you only have to compile .bashrc by this
$ source ~/.bashrc
After this you can only have to use firefox instead of long /home/debian/firefox/firefox
Adding /home/debian/firefox to your PATH should have done it.
Did you start a new shell after making that change? Otherwise the new PATH would not have exported yet. Alternatively, you can just run export PATH=$PATH:/home/debian/firefox directly to update it for your current session.

How to use a different .bashrc

I got the common .bashrc in my /home/ folder. But I have another .basrch (.bashrc1) (I have a lot of aliases) I cannot copy the content from one to another. So. I want to know if there is a possibility to use the .bashrc1 as default or if there is an additional command to execute the aliases that are into the .bashrc1
Thanks
In your .bashrc, put
source /path/to/.bashrc1
To force bash to use a different .bashrc (bad practice)
mv ~/.bashrc ~/Bob/
bash --rcfile ~/Bob/.bashrc
For example, if you use GNOME, add a custom keyboard shortcut with above command.

linux: passing arguments to the .bashrc file

I want to be able to run bash in different predefined configurations. for example: When i'm at work i'll do something like:
bash work
And it will load the appropriate environment variables.
The way i see it, the best way of doing it is passing the configuration name to the .bashrc command and than just configure it according to the name.
To do that, i need to pass the name to the .bashrc file.. How do i pass arguments to the .bashrc file from the bash command?
Of course alternatives for doing what i want will be appreciated!
Thank you!
From the man page:
--rcfile file
Execute commands from file instead of the standard personal initialization file ~/.bashrc if the shell is interactive (see INVOCATION below).
I don't think you can pass arguments to your individual bashrc directly, but you can call a common bashrc from your customized rc files and then either use arguments or global variables.
If you just want to set some environment variables, you can of course just manually call your customised rc file. To make this a little nicer from your command line, you can define a respective alias:
alias load_work='~/.workrc'
or with a new bash
alias load_work='bash --rcfile ~/.workrc'

what is .bashrc - How to find the startup file - putty

I was going over this article and it states in step 3
Add the following to your .bashrc (or the appropriate startup file for your shell) To use it immediately, be sure to type “source .bashrc”
Any idea on how I could know what my startup file is ? I am using putty ?
Once you use putty to SSH into your server, you can run "ls -al .bashrc" and it should show you the file, edit this with an editor you know, if none, then use vi like this "vi .bashrc".
Go to where you need to edit the file and type in "i" to put vi in Insert mode. Next type in your text. Once you are done press the escape button and ":wq", no quotes for the i or :wq.
Next you can source it by typing "source .bashrc" and the setting you added should be part of your BASH shell environment now.
The .bashrc is a file which is called by bash before on each start of a new interactive shell. The file can be used to setup the environment, export variables, create aliases and functions and more...
There are usually multiple instances of that file. One per system and one per user to allow system wide configuration but also customization by users ( users bashrc will be sourced after the system wide bashrc and can overwrite things). I suggest to add the lines to your user's bashrc first. The file is located in your home folder. Type:
vi $HOME/.bashrc
in order to edit the file. If you aren't familiar with the vi editor you can choose an editor of your choice like nano, mcedit or even a GUI text editor, but mind that a GUI editor's file dialog may hide the file because it's name starts with a .
Once you managed to edit the file, start a new connection or simply type
source $HOME/.bashrc
in order to parse the file
A path which will work with any bash shell regardless of operating system (macOS/Linux/BSD etc.) is:
~/.bashrc
check your home directory ...because it exists in user's home directory.
check /home/username/ on your terminal if you are using RHEL or CentOS.
.bashrc and .bash_profile are bash config files (bash shell script) that bash runs(execute) whenever it is started interactively. It initializes an interactive (non-login) shell session and the config is read from these files $HOME/.bashrc
.bashrc is a standard hidden file located in your home directory.It determines the behaviour of interactive shells.
.bashrc runs on every interactive shell launch.If you say: $bash
For login shells, the config is read from these files:
/etc/profile (always sourced)
$HOME/.bash_profile (the rest of these files are checked in order until one is found,then no other are read)
$HOME/.bash_login
$HOME/.profile
For example: I added an echo to my .bashrc and .bash_profile files and whenever I called bash or bash -l command in terminal it showed me the echo.

Resources