Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commands from .bashrc not available in Emacs

Tags:

bash

emacs

I have some aliases and functions defined in ~/.bashrc.

I start emacs from a terminal window using emacs -nw

When I execute M-x shell-command, the aliases and functions from ~/.bashrc are not available, but give a "command not found".

I've googled quite a bit but all the posts I come across say, if I understand them correctly, that ~/.bashrc is the place where this should work (rather than ~/.profile or ~/.bash_profile).

What am I missing?

like image 939
Marianne Avatar asked Sep 08 '09 11:09

Marianne


People also ask

How do I view a .bashrc file?

From a login or other node on the cluster, type nano ~/. bashrc to open the file in the nano editor. My . bashrc has already been added to, so you'll see additional definitions below the # User specific aliases and functions section.

How do I run a shell command in Emacs?

You can execute an external shell command from within Emacs using ` M-! ' ( 'shell-command' ). The output from the shell command is displayed in the minibuffer or in a separate buffer, depending on the output size.

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

In short, you are putting your aliases in the wrong file . bashrc , that is why you need to keep running source to get the aliases working in any new login terminal instances.

How do I access Emacs shell?

To enter Emacs, type emacs at the shell prompt. When you want to leave Emacs for a short time, type a C-z and Emacs will be suspended. To get back into Emacs, type %emacs at the shell prompt.


2 Answers

Aliases are available only in interactive shell - a snapshot from bash man page:

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

like image 136
dimba Avatar answered Sep 22 '22 21:09

dimba


If you start Emacs from an interactive shell (in which .bashrc, etc. have executed), then the commands from your .bashrc should be available for both shell-command and shell, at least that's how it works for me.

But yeah as the other answer says, there is not real way to get a .bashrc environment in emacs. There are some documentation about a .emacs_bash file, but that never worked for me.


Okay misread your question here. If you are looking for functions and aliases instead of commands by changing paths in .bashrc, the non-interactiveness is the problem. I guess you can change the default argument to shell-command (take a look at explicit-bash-args) to make bash interactive, but that probably has unintended consequences.

like image 20
polyglot Avatar answered Sep 22 '22 21:09

polyglot