72

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!

o_O
  • 5,527
  • 12
  • 52
  • 90
  • a) Do you get «command not found» in interactive session or in a script. b) Does the alias work if defining command typed (i.e. `alias ide=…ide`) in the interactive session? c) What is in your `.zshrc`. d) Does this work if you launch `zsh -i` in an interactive session with updated `zshrc`? – ZyX Jan 11 '13 at 21:46
  • I didn't source after making changes...sorry. – o_O Jan 11 '13 at 21:47
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww May 09 '18 at 04:02
  • In my case it was a broken ~/.zshrc file due to וnnecessary apostrophe ('). – EladTal Aug 13 '23 at 12:30

15 Answers15

156

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.

Community
  • 1
  • 1
ShawnW.
  • 1,771
  • 1
  • 12
  • 12
  • 3
    Single/double is irrelevant unless string contains some special characters (like `$` and «\`»). – ZyX Jan 11 '13 at 21:41
  • 7
    I found the problem a few minutes ago. I am not experienced enough to know that I need to source after making changes to shell rc. I just assumed that like changes to .vimrc and the like that the changes were immediate. So now that I've sourced, all is well. Thanks for coming in even though it's such a simple mistake on my part. – o_O Jan 11 '13 at 21:46
  • 2
    @RKS Changes to `.vimrc` are immediate only if you have some vim plugin (or some code in vimrc) that make them be immediate. It is not the default behavior. It is not impossible to write code that serves same purpose for zsh. – ZyX Jan 11 '13 at 21:50
  • show, tested with browsers on macOS using alias firefox= "open -a firefox" – Flávio Apr 17 '21 at 22:59
  • this isn't working for me either: `> alias hello="echo hello" > hello zsh: command not found: hello`. ;; it doesn't work when put in my `~/.zshrc` either. – Max Cascone Jan 13 '23 at 19:20
39

Add "source ~/.bash_profile" to your ~/.zsh config file.

dsteplight
  • 789
  • 7
  • 5
15
  • Put 'source ~/.bash_profile' into ~/.zshrc file

  • Save/Exit ~/.zshrc file

  • Restart the Terminal

FullStackCoder
  • 420
  • 4
  • 6
json25
  • 151
  • 1
  • 3
8

After saving changes in ~/.zshrc file, open a new shell window and execute the command in it.

Aditya
  • 1,136
  • 2
  • 12
  • 19
7

Sometimes the simple solution is what we need... Add "source ~/.bash_profile" to your ~/.zshrc config file

echo source ~/.bash_profile >>  ~/.zshrc
Ofer Rahat
  • 790
  • 1
  • 9
  • 15
4

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
Abram
  • 39,950
  • 26
  • 134
  • 184
3

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'
Jhamman Sharma
  • 147
  • 1
  • 2
3

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
codeskin
  • 147
  • 1
  • 1
  • 8
2

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.

Antonio
  • 322
  • 2
  • 11
2

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.

Mahund
  • 21
  • 3
2

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.

Ehsan
  • 3,711
  • 27
  • 30
0

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
jturi
  • 1,615
  • 15
  • 11
0

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

pfx
  • 20,323
  • 43
  • 37
  • 57
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33538178) – chrslg Jan 02 '23 at 22:27
0

For those who still struggle , none of previous solutions work for me. I finally manage to fix this, remove additional spaces around the '=' symbol. replace this :

alias test = 'my command'

to

alias test='my command'
Aina27
  • 76
  • 2
-1

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.

sunny rai
  • 565
  • 6
  • 6