23

I'm trying to setup a simple alias to move me into my Developer folder on my machine. However, after setting it up, I get a weird error:

-bash: dv: command not found

I setup my alias in .bashrc like so:

alias dv='cd Developer/'

I use it by just typing dv, and then get that error. Does anyone see any syntax errors or something I'm missing here for aliases?

Bill L
  • 2,576
  • 4
  • 28
  • 55
  • 2
    Are you sure that `.bashrc` is being read? On my machine, `.bashrc` is ignored and `.profile` is used instead. – nneonneo Oct 14 '15 at 21:30
  • I'm not 100% sure of that at all, I may try adding it to my .bash_profile instead. Edit: This is exactly what I was after, if you move your comment to an answer I'll accept it, thanks! – Bill L Oct 14 '15 at 21:34
  • 2
    In Mac OS X, terminal emulators start login shells rather than simple interactive shells because the terminal emulator itself is not (typically) started from a `bash` session that is already a login shell. – chepner Oct 15 '15 at 15:18

8 Answers8

29

Run bash and then try the command.

Alternatively, put it in ~/.bash_profile which should be loaded automatically.

pushkin
  • 9,575
  • 15
  • 51
  • 95
  • 1
    Not sure why you're being down voted as that got it to work. Is there any way to not have to type bash to get it to read the file right from the get-go? That's really what I would prefer. – Bill L Oct 14 '15 at 21:33
  • I don't think that's true, actually. I'm pretty sure `.profile` and `.bashrc` are both loaded only when you start your shell. – nneonneo Oct 14 '15 at 21:39
  • 3
    If you're going to manually run a command, better to re-source the dotfiles than to start a new shell (and leave the old one running). – Charles Duffy Oct 14 '15 at 21:51
  • 1
    That is to say: `exec bash` to replace the old instance of bash with a new one (understanding that this wipes out other local state in that shell -- variables, functions defined, etc), or `source ~/.bashrc` to run all commands in the ~/.bashrc with the existing shell. – Charles Duffy Oct 14 '15 at 21:51
  • Good point. Will `source` need to be run every time? Should it be placed in `~/.bash_profile`? – pushkin Oct 14 '15 at 21:52
  • `.bash_profile` is only run on login shells. It's often considered a best practice to source your .bashrc from your .bash_profile, to have its contents run in all cases; some operating systems' default dotfiles do this out-of-the-box. – Charles Duffy Oct 14 '15 at 21:52
  • @CharlesDuffy Worth noting that `source ~/.bashrc` carries the slight risk of re-executing one-time initialization code, which could (for example) append spurious entries to `PATH`, reset certain environment variables, etc. – nneonneo Oct 15 '15 at 01:42
  • 1
    @nneonneo Such variables should be set in `.bash_profile` instead of `.bashrc` for exactly that reason. `.bashrc` is sourced by any (non-login) interactive shell; it's reasonable to expect that `bash` may be executed from within an existing `bash` session. – chepner Oct 15 '15 at 15:15
  • it is added automatically just not he first time, one restart of the bash will do the trick – Vitaliy Terziev Sep 01 '17 at 13:16
13

.bashrc is only read on startup. If you just modified your .bashrc then you need to get a new shell or get your current shell to see the changes applied:

  • source ~/.bashrc in your current shell (although this may cause some startup items to run twice, which could cause other issues)
  • exec bash to get a new shell
  • just open a new Terminal window
nneonneo
  • 171,345
  • 36
  • 312
  • 383
8

Error:

-bash: alias: cd /opt/logs: not found alias log= "cd /opt/logs"

Solution :

Ensure there is no space after the = symbol

log="cd**  /opt/logs"
Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
Mohan Raj
  • 99
  • 1
  • 3
2

Make sure the following line is present inside .bash_profile

test -f ~/.bashrc && . ~/.bashrc

If not then add it to the beginning. This code is used by .bash_profile to load up .bashrc. If this line is not present inside .bash_profile then anything you type inside .bashrc will not be loaded.

Sparkzz
  • 367
  • 5
  • 14
2

Another solution is to call your command with bash using -i -c option:

bash -i -c my_alias
Lucas Coelho
  • 583
  • 6
  • 11
2

from man bash:

       Aliases  are  not expanded when the shell is not interactive, unless the expand_aliases shell option is
       set using shopt (see the description of shopt under SHELL BUILTIN COMMANDS below).

so if you want to use aliases on scripts, add this line in your script file:

shopt -s expand_aliases
ton
  • 3,827
  • 1
  • 42
  • 40
0

I had the same issue but the weirdest solution. I was copying it from Windows machine to OS X and for some reason, the spaces used were different and when I replaced them with normal spaces, it worked. It was not tabs, I have no idea what it was.

Ev0oD
  • 1,395
  • 16
  • 33
  • Probably https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings – tripleee Aug 01 '20 at 10:38
0

i had a similar issues while creating an alias for mongoDB; You can try putting the whole path inside .bash_profile in your home directory like so :

alias mongo="/c/Program\ Files/MongoDB/Server/5.0/bin/mongo.exe",

as i had done in my case; it worked perfectly after this