.bashrc variable for ./command - linux

In linux, for a command that I have to run like ./command ; how do I set a .bashrc environment variable to run the command from any directory without having to put the full path for the command.
Thanks

Just add the directory to the path.
Tutorial: Adding a Directory to the Path

You modify the PATH environmental variable like so
PATH=${PATH}:/the/directory/to/the/executable
export PATH
Note that you cannot actually include the executable, which means it's full path would look like
/the/directory/to/the/executable/command
For executables that override common utilities, to make the executable be found first, you need to reverse the order, like so
PATH=/the/directory/to/the/executable:${PATH}
export PATH

Related

Error in setting the path variable in Linux

I would like to set a path to the Gnu toolchain downloaded from https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads. I used the line
$PATH=$HOME/Toolchains/gcc-arm-none-eabi-9-2020-q2-update/bin:$PATH to set the path in the terminal.
For the first time, I was able to change the path, then I closed the terminal and again set the (wrong) path $PATH=/home/paulson/Toolchains/gcc-arm-none-eabi-9-2020-q2-update/bin:$PATH. Now I am unable to set the correct path and the terminal shows as below.
paulson#debian:~/Toolchains$
paulson#debian:~/Toolchains$ ls
gcc-arm-none-eabi-9-2020-q2-update
paulson#debian:~/Toolchains$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
paulson#debian:~/Toolchains$ $PATH=$HOME/Toolchains/gcc-arm-none-eabi-9-2020-q2-update/bin:$PATH
bash: /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games=/home/paulson/Toolchains/gcc-arm-none-eabi-9-2020-q2-update/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games: No such file or directory
If you want to set the path environment variable, you need to use:
export PATH=/some/path:$PATH.
In your case, it would look like:
export PATH=$HOME/Toolchains/gcc-arm-none-eabi-9-2020-q2-update/bin:$PATH
The only downside to setting the path manually is that it will not persist across bash sessions. If you want this change to be permanent, then I would suggest adding that line to your .bashrc or .profile file in the home directory.

How to make command "Hadoop" work in linux 18.04?

I installed Hadoop and now it works with command /usr/local/hadoop/bin/hadoop.
Where and how should I add this path to make command hadoop work without a full path to file? I already tried .bashrc and /etc/environment, but it didn't help.
You need to add it to your PATH environment variable. PATH is used to find programs to run in the shell.
To see what your current PATH is you can type
$ echo $PATH
When you update the PATH variable you want to make sure you don't delete previous entries.
$ PATH=$PATH:/usr/local/hadoop/bin

Executing a script in a different directory without having to use the full path

I generate a bash script named "script1" and place it in the directory /home/Me/bin. I want to execute it (just by calling it's name ./script1) without the complete path from any directory. How can I do it? Is there any specific command?
The directory the script is in needs to be included in your PATH environment variable. $HOME/bin usually is included already. Otherwise, you can add it in your ~/.bashrc:
PATH=$PATH:/home/Me/bin
Add "/home/Me/bin" to your PATH.
Then, you can just call "script1".

How to add multiple path in $PATH variable linux?

I want to add multiple path in $path variable like java path and php path . how to do that in linux?
I am doing something in bash_profile like :
PATH=$JAVA_HOME/bin:$PATH:/usr/java/jdk1.7.0_45/bin/:$AWS_AUTO_SCALING_HOME/bin
$PATH can have several paths separated by a colon (:). E.g.:
export PATH=/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/myuser/bin
Set the $PATH environment variable to include the directory where you installed the bin directory with shell scripts and two consecutive export entries as in example.
Example:
export ANT_HOME=/path/to/ant/dir
export PATH=${PATH}:${ANT_HOME}/bin:${JAVA_HOME}/bin
To make them permanent entries update the bash_profile file.
One way to add multiple executables to the $PATH variable is:
export PATH=/path/to/executable1:\
/path/to/executable2:\
/path/to/executable3:\
/path/to/executable4
If a $PATH already exists in .bash_profile, and you want them to take precedence over executables (like java and php), you can do:
export PATH=$PATH:/path/to/executable1:\
/path/to/executable2:\
/path/to/executable3:\
/path/to/executable4
If the path to any executable contains whitespaces, add the part / ... executableX in quotes.
Once you're done making changes in your bash_profile, source the file in a terminal session so that changes are effective immediately:
source .bash_profile
Open Terminal and Type sudo gedit /etc/profile to open system path file
Go to Bottom
VARIABLE_NAME1=/your/path/to/location1
VARIABLE_NAME2=/your/path/to/location2
PATH=$PATH:$VARIABLE_NAME1:\$VARIABL3_NAME2
export PATH
Logout from user and Relogin
If you're on a Mac, the best way in my opinion would be following Chamindu's answer with a slight tweak. using nano or vim whichever you prefer, but I'll use nano as it's easier for most people.
Open Terminal and Type nano ~/.bash_profile to open bash profile.
At the top, type or copy and paste the following:
FLUTTER="/Users/MyUsername/development/flutter/bin"
VSCODE="/Applications/Visual Studio Code.app/Contents/Resources/app/bin"
PATH=$PATH:$FLUTTER:\$VSCODE
export PATH
If you're using nano as I suggested, do a control + x on your keyboard to exit.
press Y to save your changes.
In the terminal type source ~/.bash_profile to refresh your bash profile/environment variables.
Now you can navigate to any directory and call the files in your path.
Note:
Swap out FLUTTER and VSCODE for your variable names of choice.
You would only need to use sudo if you're not using the admin account.
sudo CPATH=/usr/include/linux/:/usr/src/linux-headers-5.17.0-1-common/include/linux/ vmware-modconfig --console --install-all

Add a bash script to path

I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.
The script is quite simple is about giving apt-get access through a proxy I made it like this:
#!/bin/bash
array=( $# )
len=${#array[#]}
_args=${array[#]:1:$len}
sudo http_proxy="http://user:password#server:port" apt-get $_args
Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.
My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.
Try this:
Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
Then you can just run apt-proxy with your arguments and it will run anywhere.
Note that if you export the PATH variable in a specific window it won't update in other bash instances.
You want to define that directory to the path variable, not the actual binary e.g.
PATH=$MYDIR:$PATH
where MYDIR is defined as the directory containing your binary e.g.
PATH=/Users/username/bin:$PATH
You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.
Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.
I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some trojan variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.
As a final step, after following the solution form proposed by #jlhonora (https://stackoverflow.com/a/20054809/6311511), change the permissions of the files in the folder "~/bin". You can use this:
chmod -R 755 ~/bin
make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.
adding to #jlhonora
your changes in ~./bashrc or ~./zshrc won't reflect until you do
source ~./zshrc or source ./bashrc , or restart your pc

Resources