bash alias disappears when opening new terminal [duplicate] - bash

This question already has answers here:
Should aliases go in .bashrc or .bash_profile? [duplicate]
(4 answers)
Closed 4 years ago.
im setting up a new alias by typing this command:
vi ~/.bashrc
and then placing my alias:
alias school='ssh -Y username#linux.student.cs.uwaterloo.ca'
followed by exiting the file using: wq
however when i close my terminal and open my terminal, i get a "command can't be found." error message.
if i type source ~/.bash_aliases, it will work, the alias will work, but when i open a new terminal it won't.
is my .bashrc supposed to be empty when i vi into it?

The reason your alias is getting lost is because you dont have your bashrc sourced in a new terminal.
Same will happen even if you create a new alias file and source it in bashrc because its scope gets limited to the terminal you are editing in.
What you can do is logout once and then log in back so that bashrc entries gets updated for your user account or you can source in each terminal by typing
source ~/.bashrc
By adding the same entry to '''.profile''' you are making sure the alias is set on each system boot.
So its better to set the alias in .bashrc rather than .profile
Another major point to nite here is to make sure you dont delete anything in bashrc since that will do catastrophic changes to you session.

Related

permanently source in terminal (Mac OS)

this is probably a very basic question. I am using the bash shell still (Catalina). And I downloaded gromacs. Every time I open a new terminal window I first have to put
source /usr/local/gromacs/bin/GMXRC
in first, otherwise I will just get command not found.
How can I permanently source this so that I don't have to reenter it all the time?
I tried editing the /etc/paths file but that didn't work.
If you are still using the bash shell on macOS, all you have to do is:
Put the line:
source /usr/local/gromacs/bin/GMXRC
at the end in .bash_profile.
.bash_profile is in home directory, so you can open it like:
open ~/.bash_profile
Why .bash_profile?
.bash_profile is executed whenever a login interactive shell starts and on mac, every interactive shell is a login shell by default.
If in case you switch to zsh, instead of .bash_profile, I think .zlogin should be used.
But that would depend entirely on your need. Read about zsh configuration files in case you ever need that.

How do I make my bashRC source permanent? [duplicate]

This question already has answers here:
OSX Terminal not recognizing ~/.bashrc and ~/.bash_profile on startup
(1 answer)
What's the difference between .bashrc, .bash_profile, and .environment?
(7 answers)
Closed 3 years ago.
When I make my bashRC file with nano ~/.bashrc.
With only one alias:
alias c="clear"
I only can use the alias if I enter source ~/.bashrc or use . ~/.bashr, but I want this source to carry over to new terminal windows. Is there away to make this the default source? Or is there another way to make the bashrc permanent?
Add it to .bash_profile.
.bashrc is for noninteractive logins
if you are opening a new window and logging in manually you need to add it to .bash_profile

Alias not working in Ubuntu 14.04 [duplicate]

This question already has answers here:
How to set an alias inside a bash shell script so that is it visible from the outside? [duplicate]
(4 answers)
How to reload .bashrc settings without logging out and back in again?
(18 answers)
Closed 6 years ago.
So I have added an alias for jumping to another directory like this
in my ./bashrc file , Looks like this
alias crmx="cd /var/www/crm/website-crm/"
Then i saved the file
but when I try to run
crmx
it says
command not found
Also I tried to do alias to see all the command but my command is not listed
Any idea ?
The file is ~/.bashrc (starting with a dot).
And you have to source it (reload) by doing source ~/.bashrc or just by closing and reopening your terminal.
You can also type ps to check if your shell is bash (for example if it's zsh you have put your alias in the ~/.zshrc file)
Let's suppose you are on bash.
Once you saved that file, you have to have bash read it with a command like this:
. ~/myaliasfile
or like this
source ~/myaliasfile
if the file resides in your home directory. Specify the path (relative to your home or absolute) otherwise.
Then you'd go to your .bashrc file and add the very same line to the end of it. By doing so the alias(es) will be read and made available to every single bash invocation and login.
Done!
More details here and here.

how to remove this [Bash], everytime I open Terminal? [duplicate]

This question already has answers here:
Error message on Terminal launch [duplicate]
(2 answers)
Closed 6 years ago.
bash: /etc/profile.d/sm.sh: No such file or directory
bash: /etc/profile.d/rvm.sh: No such file or directory
Alright guy's im close to solving my problem
I just used
sudo open /etc/profile
and I found these two lines, when i try to edit, it says you do not have permission to make changes in this file and ask for making a duplicate...
just tell me how to comment these lines with admin privileges.
Solution which worked:
"your last answer worked! I meant to say this I did:
sudo nano /etc/profile
put # before the two paths
press command x saved the file"
From the bash man page
FILES
/bin/bash
The bash executable
/etc/profile
The systemwide initialization file, executed for login shells
/etc/bash.bashrc
The systemwide per-interactive-shell startup file
/etc/bash.bash.logout
The systemwide login shell cleanup file, executed when a login shell exits
~/.bash_profile
The personal initialization file, executed for login shells
~/.bashrc
The individual per-interactive-shell startup file
~/.bash_logout
The individual login shell cleanup file, executed when a login shell exits
~/.inputrc
Individual readline initialization file
You will want to check the systemwide files and see if they are referencing rvm. If you are the only user on your system (it is your workstation or your home computer), feel free to modify these files to remove the rvm reference (assuming you aren't using rvm anymore). Otherwise, ask your system administrator to fix the execution environment.
More information on rvm can be found here, and your online manual pages can provide more details about what files bash reads by default.
Failing all of the above, a quick and dirty hack would be to execute
touch /etc/profile.d/rvm.sh
as a user with the appropriate permissions.

Removing no such file command from OSX terminal startup [duplicate]

This question already has an answer here:
How to restore .bash_profile on a mac? None of my unix terminal are working [closed]
(1 answer)
Closed 8 months ago.
Whenever I start a new Terminal session, the window opens up with a command:
'-bash: cd: /Users/yourusername/Library/lejos_nxj: No such file or directory'.
I want to remove that line from coming up on the prompt, so I deleted the Terminal plist file and sent hard resets, but nothing has worked for me so far.
Is there anything I haven't tried yet?
You've probably got something in your ~/.profile, which is automatically executed when you open a Terminal.
If you did not set anything in there, do
rm ~/.profile
in a Terminal and this should disappear.
If you think that something important may be in there, do
open -a TextEdit ~/.profile
(or change TextEdit to your preferred editor) to view and edit the file.
That line is probably in either your ~/.bashrc file or your ~/.profile file. I would check those two files first (and they are both hidden because of the period initial character).
Edit: I just checked my OS X system, and I don't have ~/.bashrc (but you may).
You could try a terminal replacement (Just recently read about this one, iTerm2) it might
resolve your problem by using its own default startup settings.
If it is anything like Linux the ~/.bashrc and ~/.profile are used when you open your terminal.
It seems that most people assume it is in some init file but its not clear which are included. Once your shell does come up (ignore errors) -try this:
script
bash -l -x
exit
exit
Now you should have a file called typescript which will have debug info showing most if not all of what the shell has executed when coming up in login mode. You could for instance do grep source typescript to see which files were involved OR you could find refrences to nxj etc.
p.s. I assume your clearing of the plist would clear the preferences, but wouldn't hurt to see if the relevant command is in 'Terminal-> Presences->Settings->Shell->Run Command' for any of the terminal window presets

Resources