My Bash aliases don't work - bash

I'm not sure why but my Bash aliases don't seem to work. Here is my .bashrc file
# v 0.0.1 - 7/03/12
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
PATH=$PATH:$HOME/.rvm/bin # Add RVM to PATH for scripting
# expanding history to 10000 commands
export HISTSIZE=10000
# don't store repeated commands more than once
export HISCONTROL=ignoredups
# where to look for Java
export JAVA_HOME=/Library/Java/Home
# tomcat server configuration
export CATALINA_HOME=/usr/local/apache-tomcat-6.0.35
# default editor
export EDITOR=vim
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Here is my .bash_aliases file
# v 0.0.1 - 7/03/12
# aliases for directory traversal
alias ..='cd ../'
alias ...='cd ../../'
alias ....='cd ../../../'
alias gs='git status '
alias ga='git add '
alias gb='git branch '
alias gc='git commit'
alias gd='git diff'
alias go='git checkout '
alias gk='gitk --all&'
alias gx='gitx --all'
alias got='git '
alias get='git '

Add this to the end of your .bashrc:
if [ -f $HOME/.bash_aliases ]
then
. $HOME/.bash_aliases
fi

I had a similar problem recently. The solution appeared to be closing ALL open shells (root and user; I didn't notice that I was running a minimized root shell at the time while editing my user .bashrc and .bash_aliases files). The .bash_aliases file then seemed to get read.

By default
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
These are available in your .bashrc file in ubuntu 18,19
Actually the problem is sourcing the files, therefore source both files by runing the commands below. I faced the same issues and that is how i solved it.
source ~/.bashrc
source ~/.bash_aliases

Bash doesn't look for a file called .bash_aliases; you have to source it explicitly.
Looking around a bit, it appears ~/.bash_aliases is sourced from the default .bashrc on Ubuntu boxes; I don't have access to one to confirm. However, it is not a standard bash configuration file.

I recently installed RVM and changed my terminal profile to "run command as login shell". This disabled .bashrc from loading.
Fix: edit -> profile preferences -> Title and Command -> Run command as a login shell (uncheck)
Find this post for more information, fixed it for me.
https://askubuntu.com/questions/161249/bashrc-not-executed-when-opening-new-terminal

Sometimes forgetting to source the bashrc also creates this problem. So after adding your aliases don't forget to source it.
source ~/.bashrc

You need to include the file. Example code to do so from a default .bashrc file is below:
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi

don't forget to
chmod 600 ~/.bash_aliases
on kubuntu 22.04
;)

It may be something simple. Like you are actually running zsh or korn instead of bash. Check your shell. I have done this on installing and testing various flavors. Wasted so much time I now never assume I am on bash anymore.

Related

Trying to understand Bash file setups/structure

This is an extension of a previously asked question: What are some common HDFS commands that can be mapped in the bash files?
I notice that the .bashrc and .bash_profile that I was initially provided are slightly different that what you have provided. Is this OK or some kind of different pattern?
The files that were copied over when I started are as follows:
.bashrc
.bash_profile
.bashrc
source /etc/bashrc
...and a lot of other folder mappings
.bash_profile
# .bash_profile
# Get the aliases and functions
#if [ -f ~/.bashrc ]; then
# . ~/.bashrc
#fi
source ~/.bashrc
I created the .bash-aliases file as you recommended.
.bash_aliases
alias h="hdfs dfs"
I have modified the .bashrc file as follows
.bashrc - Modified
source /etc/bashrc
...and a lot of other folder mappings
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
The .bashrc code will be executed each before a new shell is started. The code that is in there doesn't matter, as long as it is valid bash.
The other files (bash_aliases) are just here to separate the commands in different files.
This article explains it quite well: https://ss64.com/bash/syntax-bashrc.html
So to answer your question, it won't cause problems at all. All that matters is that the way it's done satisfies you.

Why must I source .bashrc every time I open terminal for aliases to work?

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...

Issue with bashrc file

When I open the .bashrc file using gedit, or vi editor, there's nothing in it. I am on MINT trying to install Hadoop and one of the step requires editing the .bashrc file, to save the environment variable. I remember doing the same on Ubuntu, and there were contents were in it.
Can you help me get them all back? Is it possible to get them all back?
When I do gedit ~/.bashrc it opens to a blank page.
Thanks in advance!
From looking at the MINT forums, it doesn't look like MINT supplies a default .bashrc:
http://forums.linuxmint.com/viewtopic.php?f=90&t=130358
However, .bashrc is an optional file, so you can add whatever you want to it. Check out the bash man pages:
man bash
Also, if you setup a .bashrc file, make sure it is getting called from either .profile or .bash_profile like this:
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You should have one in here:: /etc/skel/.bashrc
Try copying it to your home folder:: cp /etc/skel/.bashrc ~/.bashrc
Look for .bashrc.swap files in your home directory by doing so:
ls -Fa ~ | grep '*bashrc*'
I don't know how gedit or other programs name their swap files, but just look what comes up.
If there are no such files, you're out of luck.

Appending bash alias to .bashrc doesn't work

I want to create a alias of my cd command. I have created the .bashrc file and append the command cd ...... to it. (Since the file was newly created, it just has this one line that I added).
After that, only after I typed . ~/.bashrc, can the alias works. If I close the terminal and open it again, I need to retype . ~/.bashrc.
It's really annoying to do this every time. Is there any way to solve this problem?
Thank you so much for help
When you login to linux system, only ~/.profile will be called:
$ cat ~/.profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You need to source ~/.bashrc inside ~/.profile manaully.
Read this to learn more.
EDIT:
If you're using iTerm2 on mac, it actually start a login shell by default when open tabs.
But you can change it: Preferences > General > Command
If using OS X, append the alias to ~/.bash_profile.
You could also add alias to ~/.bashrc, then add source ~/.bashrc to ~/.bash_profile.
Better yet, put all your aliases in ~/.aliases, and source it in ~/.bash_profile.
By default, OS X first sources /etc/bashrc (which shouldn't be modified unless absolutely necessary), then sources the user's ~/.bash_profile at the start of every interactive session.

ZSH not recognizing my aliases?

Using iTerm2 with zsh and it isn't recognizing my aliases. Sometimes I have to work in an IDE and can't just easily vim something and the stupid people thought it a good idea to name their applications like MyReallyLongApplicationName.app and since .html files open by default in browsers, I have to:
open -a MyReallyLongApplicationName.app something.html
I have an alias in my .zshrc like:
alias ide="open -a MyReallyLongApplicationName.app"
But zsh won't recognize my aliases. I tried another one just to see if it was me but none of the aliases I create are recognized. Just get "zsh: command not found: ide" or whatever.
So I'm not sure what I'm doing wrong and I've been searching around all day trying to fix things in zsh and the like. As a note, I'm not a pro at Linux/Unix systems so if you're too technical and vague I probably won't understand what you're telling me.
Thanks!
if you do a very simple alias in zsh, does it work? open your .zshrc file, and add the following line:
alias ls='ls -GpF'
after adding that line, type this line in your Terminal:
source ~/.zshrc
tell us what happens. Also, just for shiggles, make sure you are using single quotes vs. double quotes, I have seen that make a difference in the past on different versions of shells/OS/whatnot.
Add "source ~/.bash_profile" to your ~/.zsh config file.
Put this line:
/source: 'source ~/.bash_profile' into ~/.zshrc
After saving changes in ~/.zshrc file, open a new shell window and execute the command in it.
Sometimes the simple solution is what we need...
Add "source ~/.bash_profile" to your ~/.zshrc config file
echo source ~/.bash_profile >> ~/.zshrc
I needed to manually add the alias to my zsh config file and then run the source command on it.
echo alias this='some command' >> ~/.zshrc
source ~/.zshrc
In my case issue was space b/w aliasName and equalTo. you should have to remove those space.
bad assignment
alias keu = 'k exec -it utils bash'
correct one
alias keu='k exec -it utils bash'
What I did, in this case, was created a separate file to store all my aliases. I found this way to be cleaner and easier maintained.
My aliases file is simply called aliases and within my .zshrc I have the following:
# Linking my aliases file
source ~/foldername/aliases
Make sure the double quotes are actual double quotes and not some other character which looks like double quotes.
I was editing ~/.zsh-aliases in OSX - TextEdit, which, when hitting the double quotes key substituted it for another special double quotes character, which is not what ZSH expects.
After editing the alias file with Sublime and replacing the old double quotes with actual double quotes everything runs just fine.
Hope this helps.
I had all my aliases on ~/.bash_profile, so i added at the last line on ~/.zshrc the following line: . ~/.bash_profile and it worked for me.
You should put alias at the end of ~/.zshrc file.
you can use below command to do that:
echo alias this='some command' >> ~/.zshrc
after that run
source ~/.zshrc
then, open a new terminal and execute the command in it.
I'm using both bash and zsh with one .bashrc, .bash_aliases and .zshrc file.
Put this in you .zshrc to load bash files:
# shortcut to refresh .zshrc
alias refz="source ~/.zshrc"
# Load bash files to zsh
test -f $HOME/.bashrc && . $HOME/.bashrc
test -f $HOME/.bash_aliases && . $HOME/.bash_aliases
If you have many bash aliases and functions you may will have some error messages like:
/proc/self/fd/13:12310: bad option: -t
caused by bash specific lines in.bash_aliases or .bashrc files
You can skip those problematic ones using:
if [ -n "$BASH" ] ;then
lines to ignore by zsh
fi
For example kubectl autocompletion
# To fix error massage .bashrc:16: command not found: shopt
# Check if bash is the current shell, if not, skip it
if [ -n "$BASH" ] ;then
# kubectl and bash completions
if [ -x "$(command -v kubectl)" ]; then
source <(kubectl completion bash)
complete -F __start_kubectl k
fi
if ! shopt -oq posix; then
if [ -f /etc/profile.d/bash_completion.sh ]; then
. /etc/profile.d/bash_completion.sh
fi
fi
fi
# Instead I need to put this line somewhere in my zshrc
# to have kubectl autocompletion replacing the skipped bash one:
plugins=(git git-flow brew history node npm kubectl)
# To fix error message .bash_aliases:4: parse error: condition expected: =
# Change these to this syntax to be used by zhs
# Not compatible with zsh:
if [ $HOSTNAME = "x1" ]; then
# Compatible with bash and zsh:
if [[ $HOSTNAME == "x1" ]]; then
I had a bad alias line that should have been obvious.
There is no error checking in these zsh custom scripts, so you may, like me, waste a couple of precious hours trying to find out why your custom aliases or functions are not loading in iTerm2.
I am using MacOS Ventura 13.1 and iTerm 2 v3.4, zsh 5.8.1.
I reloaded and using . ~/zshrc and followed a lot of the suggestions above.
I finally copied my list of aliases and function to the ~/.zshrc file.
Another ~/.zshrc gave me a bad pattern error.
The fix for my case was as follows.
In line with a Unicode string or something that is escaped and requires ' ' (single quotes), you must use the other quote character (double quotes) to encapsulate the alias sting.
In my case, I had entered.
alias fixitemerrow='printf '\e[?2004l''
The fix for this is:-
alias fixitemerrow=" printf '\e[?2004l' "
Do not add spaces in your alias assignment. This is here just for illustration.
Spaces would also require double encapsulation " ' ' ".
Need to create a profile for .zshrc and register the alias into it. (only if the profile isn't available)
cd ~
touch .zshrc && open .zshrc
add all the alias in .zshrc file
source ~/.zshrc
close and re-open terminal and run the alias.

Resources