I regularly use the command SOURCE to be able to open a software. So I type :
source /usr/blabla/blabla
softwarelaunch
I wonder if it is possible to call the source command everytime I launch a terminal. So I would only have to launch et terminal and to type softwarelaunch.
This is what the file ~/.bashrc is for!
This file gets sourced every time you open a terminal window. If you want to execute /usr/bla/bla then you can add a line at the end of the ~/.bashrc file:
source /usr/bla/bla
Add something like this to your ~/.bashrc:
if [ -f /usr/local/foo/bar ]; then
. /usr/local/foo/bar
fi
Related
I want to use the last exit code to customize my bash prompt. So following this question I should add $? inside a function:
PROMPT_COMMAND=__prompt_command
__prompt_command() {
local EXIT="$?"
...
I tried many times, but whenever I add a function on my C:\Program Files\Git\etc\profile.d\git-prompt.sh file, Git Bash can't run this file, and it doesn't use it.
Is there a way to use a function on git-prompt.sh on Git Bash for Windows, or another approach to use the last exit code to customize the bash prompt?
Follow these instructions to configure your git-bash prompt.
First, open git-bash, and cd to your ~ directory.
Using vim or nano, edit your ~/.bash_profile file to contain the following two lines:
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc
Then create a ~/.bashrc file and add your custom prompt command function in this file:
PROMPT_COMMAND=__prompt_command
__prompt_command() {
local EXIT="$?"
# The rest of your code here
}
Make sure your changes saved correctly for both your ~/.bash_profile and ~/.bashrc files.
Open a new git-bash window. Assuming you have no bugs in your __prompt_command() function, your custom prompt should display properly.
If you quickly want to test your prompt, you can instead just run source ~/.bashrc in your current git-bash window rather than launching a second one.
Git was working fine. I have created an alias in Git but the issue is when I tried to reopen the terminal, then I need to run . ~/.bashrc every time in the terminal.
What is the best way I don't need to provide source every time when I reopen the terminal?
What I did?
I am trying to add source of the .bashrc file in this file but it is a read-only file. I am not able to add the source of the .bashrc file in this profile.
open /etc/profile
Added the permission to write in the profile as well, still not able to link the source file.
sudo chmod u+w /etc/profile
Profile:
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
It looks like your terminal emulator is launching bash as a login shell.
If that's the case, it will read /etc/profile for configuration as well as 1 of the following files, if they exist (listed in order of importance) :
~/.bash_profile
~/.bash_login
~/.profile
It will thus ignore your .bashrc file. A correct fix for your situation would be to either configure your terminal emulator to run bash interactively and non-login, or add the following line to your ~/.bash_profile :
[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"
Here is a link to the documentation about which files are loaded depending of the type of shell you are running
As per #Aserre's answer i have followed this step to solve this issue
A typical install of OS won't create a .bash_profile for you. When you want to run functions from your command line, this is a must-have.
Start up Terminal
Type cd ~/ to go to your home folder
Type touch .bash_profile to create your new file.
Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profile to open it in TextEdit.
[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc" Save it and close it
Restart the terminal, It should work
You should write this line source .profile inside your .zshrc file. This is because default shell is zsh. If u don't want to do this solution than u can go for changing the default shell by typing the following command chsh -s /bin/bash then restart your machine or virtual machine. Then no need for source. I hope this will help :) TAKE CARE
If you are using Linux and you want variables set, to persist.
Follow the below steps.
Be the root user -> sudo su
go to etc folder -> cd /etc
open the file bashrc with the editor of your choice -> vi bashrc
set the variable with export command like here I am setting JAVA_HOME ->
export JAVA_HOME=pathHere
Load the bashrc file with command ->
. bashrc
remember to put the dot/period before bashrc.
now JAVA_HOME should be set permanently.
Thanks...
So I'm installing some things for coding and personal usage, and I need to run this in the terminal (I'm on Mac if you didn't read the title).
~/.bash_profile
It just says permission denied, Im running OSX 10.8.4 Mountain Lion. How do I bypass this?
On MacOS: add source ~/.bash_profile to the end of ~/.zshrc.
Then this profile will be in effect when you open zsh.
You would never want to run that, but you may want to source it.
. ~/.bash_profile
source ~/.bash_profile
both should work. But this is an odd request, because that file should be sourced automatically when you start bash, unless you're explicitly starting it non-interactively. From the man page:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
If you change .bash_profile, it only applies to new Terminal sessions.
To apply it to an existing session, run source ~/.bash_profile. You can run any Bash script this way - think of executing source as the same as typing commands in the Terminal window (from the specified script).
More info: How to reload .bash_profile from the command line?
Bonus: You can make environment variables available to OSX applications - not just the current Bash session but apps like Visual Studio Code or IntelliJ - using launchctl setenv GOPATH "${GOPATH:-}"
As #kojiro said, you don't want to "run" this file. Source it as he says. It should get "sourced" at startup. Sourcing just means running every line in the file, including the one you want to get run. If you want to make sure a folder is in a certain path environment variable (as it seems you want from one of your comments on another solution), execute
$ echo $PATH
At the command line. If you want to check that your ~/.bash_profile is being sourced, either at startup as it should be, or when you source it manually, enter the following line into your ~/.bash_profile file:
$ echo "Hello I'm running stuff in the ~/.bash_profile!"
No need to start, it would automatically executed while you startup your mac terminal / bash. Whenever you do a change, you may need to restart the terminal.
~ is the default path for .bash_profile
I was getting this error on zsh(mac os Big Sur 11.3), This is how i solved this :-
Go to Terminal.
cd /users/<yourusername>
Once you reach here issue a command :
ls -al
You will see a lot of files and one specific file .zprofile. This is your user profile. We need to edit this.
After this we need to edit the file. Issue the below command :
nano .zprofile
Once you issue this command file will be opened for edit. Add the path details for maven.
M2_PATH="/Users//code/apache-maven-3.8.1/bin" //add your path of maven diretory
PATH="${PATH}:${M2_PATH}"
export PATH
press ctrl + X and save the file.
Issue command after saving the file :
source .zprofile
Once done, you will be able to run the mvn command.
If the problem is that you are not seeing your changes to the file take effect, just open a new terminal window, and it will be "sourced". You will be able to use the proper PATH etc with each subsequent terminal window.
To create a file from terminal I type the following...
$ touch filename.py
To open the file I just created from terminal, I then type...
$ open filename.py
I'm curious to know if there is a terminal command that does both...create and then open (I'm super lazy).
in .bashrc
lazytouch()
{
touch $1
open $1
}
then type
$ lazytouch anything.really
This is as lazy as one can get:
$ echo "your text" > myfile.txt
Simplest way to do this is
touch filename; open filename
Example
touch myfile.py; open myfile.py
What I do when I want to create a file, edit it and just save it is I type vim at the terminal. vim is a text editor. If you just type in vim you would see the text editor.
But if you type for instance vim example.txt you open vim and from then on you are working in the file you created. The file does not get saved until you say so. So by pressing i you enter the edit mode of vim. Allowing you to put text in the file. If you want to save just enter escape followed by :w, meaning you are saving the file with the name you have it to it, so for this example it would be example.txt. After you saved it, everything you type after pressing Esc is shown left down in the screen, simple type :q to quite it.
If you realise you do not really want to save the file you can just type :q! and if you were currently in the editing mode, meaning you were typing something, you just press Esc once followed by :q!.
So short summary:
vim example.txt (opens the editor if saved it will use the given name)
s (will enable edit mode, you can write stuff)
Esc (when you want to stop editing)
:w (save the file)
:q (quit the file, only usable when saved!)
:q! (discard the save and just exit the file)
you can use the following to create a file named "filename.py", insert "Hello World" into the file and then open the file,
$ echo "Hello World" > filename.py && open filename.py
On a Mac to create a lazytouch function to create and open a file in one line you have to edit .bashrc. You might have to first create it. Beware if you are a novice programmer. Some of these commands might require you to prepend sudo for permission to create and save. Enter these commands in terminal.
$ cd ~
$ touch .bashrc
$ open .bash_profile
Enter this profile in .bash_profile to check for .bashrc
# To get aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
Remember to save .bash_profile. Then in bash do this.
$ open .bashrc
Enter this text in the .bashrc
# .bashrc
# User specific aliases and functions
lazytouch() {
touch $1
open $1
}
Remember to save .bashrc
Now you can cd to any folder then create and open a file with one line.
$ lazytouch anything.really
you can use:
cat -> youNewFile.someExtension
Example:
cat -> myNewFile.txt
After you are done press Ctrl + d to save or Ctrl + c to abort (but in this case it's gonna save an empty file.
The redirection operator ( > ) will create the file if it doesn't already exists in your folder and you will be able to edit it right a way through the terminal.
How can I reload file .bash_profile from the command line?
I can get the shell to recognize changes to .bash_profile by exiting and logging back in, but I would like to be able to do it on demand.
Simply type source ~/.bash_profile.
Alternatively, if you like saving keystrokes, you can type . ~/.bash_profile.
. ~/.bash_profile
Just make sure you don't have any dependencies on the current state in there.
Simply type:
. ~/.bash_profile
However, if you want to source it to run automatically when terminal starts instead of running it every time you open terminal, you might add . ~/.bash_profile to ~/.bashrc file.
Note:
When you open a terminal, the terminal starts bash in (non-login) interactive mode, which means it will source ~/.bashrc.
~/.bash_profile is only sourced by bash when started in interactive login mode. That is typically only when you login at the console (Ctrl+Alt+F1..F6), or connecting via ssh.
If you don't mind losing the history of your current shell terminal, you could also do
bash -l
That would fork your shell and open up another child process of bash. The -l parameter tells Bash to run as a login shell. This is required, because .bash_profile will not run as a non-login shell. For more information about this, read here.
If you want to completely replace the current shell, you can also do:
exec bash -l
The above will not fork your current shell, but replace it completely, so when you type exit it will completely terminate, rather than dropping you to the previous shell.
You can also use this command to reload the ~/.bash_profile for that user. Make sure to use the dash.
su - username
I like the fact that after you have just edited the file, all you need to do is type:
. !$
This sources the file you had just edited in history. See What is bang dollar in bash.
You just need to type . ~/.bash_profile.
Refer to What does 'source' do?.
Save the .bash_profile file
Go to the user's home directory by typing cd
Reload the profile with . .bash_profile
Add alias bashs="source ~/.bash_profile" into your Bash file.
So you can call bashs the next time.
If the .bash_profile file does not exist, you can try to run the following command:
. ~/.bashrc
or
source ~/.bashrc
instead of .bash_profile.
You can find more information about bashrc.
Use
alias reload!=". ~/.bash_profile"
Or if want to add logs via functions:
function reload! () {
echo "Reloading bash profile...!"
source ~/.bash_profile
echo "Reloaded!!!"
}
While using source ~/.bash_profile or the previous answers works, one thing to mention is that this only reloads your Bash profile in the current tab or session you are viewing. If you wish to reload your bash profile on every tab/shell, you need to enter this command manually in each of them.
If you use iTerm, you can use CMD⌘ + Shift + I to enter a command into all current tabs. For terminal it may be useful to reference this issue;
I use Debian and I can simply type exec bash to achieve this. I can't say if it will work on all other distributions.
I am running macOS v10.12 (Sierra) and was working on this for a while (trying all recommended solutions). I became confounded, so I eventually tried restarting my computer! It worked.
My conclusion is that sometimes a hard reset is necessary.
Simply re-sourcing the file won't "reload" in the sense that something is first unloaded, then loaded again. If that is what you want you can do:
hash -r && _SHOW_MESSAGES=1 exec -a -bash bash