26

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?

3
  • 2
    Are you sure that .bashrc is being read? On my machine, .bashrc is ignored and .profile is used instead. Commented Oct 14, 2015 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! Commented Oct 14, 2015 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. Commented Oct 15, 2015 at 15:18

9 Answers 9

31

Run bash and then try the command.

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

Sign up to request clarification or add additional context in comments.

9 Comments

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.
I don't think that's true, actually. I'm pretty sure .profile and .bashrc are both loaded only when you start your shell.
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).
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.
@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.
|
15

.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

1 Comment

I typically add [[ -f ~/.bashrc ]] && . ~/.bashrc in my profile.
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"

Comments

5

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

1 Comment

I had to source my aliases file also according to: unix.stackexchange.com/a/1498/395493
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.

Comments

2

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

bash -i -c my_alias

Comments

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.

1 Comment

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

Comments

0

Had the same issue tried it without spaces worked like a charm Example-$ alias cls='clear' Thats all i did.

1 Comment

What do you mean without spaces? Where are the spaces in the question code that should be removed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.