Environment variable PATH on linux - 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.

Related

Linux $PATH variable autoupdates

I have a problem on my Ubuntu 18.04 , I set PATH variable , and it works fine but after some time it reverts my changes. I do something like:
export $PATH=$PATH:/my/dir/bin .
You could add it to your .bashrc, and make it permanent.
Using any text editor, edit ~/.bashrc, and add the line you need.
Then source it, source ~/.bashrc.
With the export command the new PATH variable will be only available at the current terminal session. To make it permanent over the different sessions you will need to add it to the ~/.bashrc file.
That call will only work for your terminal session, you need to edit your ~/.profile or ~/.bashrc file, and put the path in there then start a new terminal session

How to not print the path in user#user:path$ in my terminal

When I'm working inside a directory in my console, this line
user#user:/a/very/very/very/very/long/path$ is disturbing me. Is there a way to minimize or delete the path from the console.
Simply export the PS1 env variable to what you want to display, example :
export PS1='$(whoami)#$(hostname):'
If you want this to be permanent, make sure to configure it in $HOME/.bashrc or $HOME/.bash_profile.
Configure the $PS1 variable to your liking. See PROMPTING in man bash for details. For example, using \W instead of \w would show only the name of the current directory, not the whole path.
export PS1='\u#\h:\W\$'
Save the line to your ~/.bashrc or ~/.bash_profile to apply the configuration to every newly started session.

Add a command for bash script to terminal

I have studio.sh file in my android-studio/bin folder, which I would like to use as a command in bash (like launching any other normal application).
I read somewhere that adding this line to ~/.profile should work,
export PATH=$PATH:/home/goel/android-studio/bin
But it doesn't work. Whats the correct process?
Add the script folder name to PATH environment variable in ~/bash.rc file
and you can also create alias for you script in ~/bash.rc
and source the /etc/bash.bashrc file, now you can issue your script or alias name in any terminal. Hope this helps.
If you change your PATH in a .profile, you still have to make the shell read the .profile. Starting a new terminal is sometimes not enough (some terminals don't read the .profile), in which case you have to log out and back in.
Is studio.sh executable? Have you tried ./studio.sh inside its containing folder to check whether it runs at all?

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.

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.

Resources