I have a command my_cmd that is aliased to another command in my ~/.bashrc. From Perl and Python, I can run this command as bash -ic "my_cmd" (this also works from the terminal window, but here it is not necessary, and I can simply type my_cmd since bash already has loaded my ~/.bashrc). When I try this from Emacs:

(print (shell-command-to-string "bash -ic 'my_cmd'") (current-buffer)))

I get error:

bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell

I am running Emacs 24.3.1 on Ubuntu 14.04.

  • 1
    I'm getting the errors, too, but I'm also getting the output of the command. But it can be solved by simply adding 2>/dev/null.
    – choroba
    Dec 11, 2014 at 15:02
  • @choroba Yes it works if adding 2>/dev/null.. Thanks! But why is it working? Dec 11, 2014 at 16:43
  • @choroba For some reason it does not work with 2>/dev/null any more.. (I get output, but I also get the error message) Dec 11, 2014 at 17:16
  • Where exactly did you add it?
    – choroba
    Dec 11, 2014 at 17:16
  • 1
    I tried (print (shell-command-to-string "bash -ic 'my_cmd' 2>/dev/null") (current-buffer)).
    – choroba
    Dec 11, 2014 at 17:49

3 Answers 3


I tried it as well. I got the errors, but I got the output of the command, too. So, a partial solution would be to redirect the errors away:

(print (shell-command-to-string "bash -ic 'my_cmd' 2>/dev/null")

Including the redirection inside single quotes doesn't help, because it's the outer bash that reports the problems, not the command.


A solution seems to be to use a login shell instead of interactive shell, according to the following answer on stackoverflow: running-system-command-under-interactive-bash-shell.

First make sure ~/.bash_profile contains:

if [ -f ~/.bashrc ]; then
    shopt -s expand_aliases
    . ~/.bashrc

You can now use bash -lc instead of bash -ic:

(print (shell-command-to-string "bash -lc 'my_cmd'") (current-buffer)))

ok so i don't know how the whole thing works. i'll try to find out interactively

googling job control gives me http://www.gnu.org/software/libc/manual/html_node/Job-Control.html#Job-Control and https://stackoverflow.com/questions/11821378/what-does-bashno-job-control-in-this-shell-mean

so what exactly does "shell-command-to-string" do? C-h f shell-command-to-string RET Then I click on the link to the file to get me source code. (Ch-f = M-x describe-function)

(defun shell-command-to-string (command)
  "Execute shell command COMMAND and return its output as a string."
      (process-file shell-file-name nil t nil shell-command-switch command))))

what is "shell-file-name", you have to check by your place but for me C-h v shell-file-name RET returns "bash". (C-h v is M-x describe-variable)

and for me shell-command-switch is "-c". So no -i be default.

[WRONG]So by default you start an interactive bash in a non interactive bash. So yeah, doesn't work. I don't know much about the whole thing so can't tell you how to fix it. Maybe changing shell-command-switch would help, I'd try that out. Maybe it opens a whole another can of worms ^^![WRONG]


well actually I was quite wrong!

bash -c "bash -ic 'll'" (with ll aliased to ls -l in .bashrc) actually works!

So I guess I'd need to know what you tried to run to test it at home :)

  • 2
    -1 for the following reason: the answer currently concludes that it doesn't work, you don't know how to fix it, and then suggests something you haven't tried. Small, speculative suggestions are welcome in comments, but answers should answer the question. The discussion of job control, shell-command-to-string, shell-file-name, and shell-command-switch, if cleaned up, would provide the possible basis for the background to an answer. I'll remove my downvote and convert it to an upvote if you can clean up the text and provide a solution to OP's problem.
    – Dan
    Dec 11, 2014 at 16:21
  • 2
    well i don't have enough rep to write comments and i think my answer helped the dude to some extent, gave him some leads. Hope he can still see it!
    – freakhill
    Dec 11, 2014 at 16:39
  • If you can clean it up and condense it, I can convert it to a comment for you.
    – Dan
    Dec 11, 2014 at 16:57
  • Thanks, freakhill. I also have shell-file-name equal to bash.. but I tried setting (setq shell-command-switch "-ic") but it still does not work :( Dec 11, 2014 at 17:01
  • unfortunately i cannot help you without more data there. bash -ic 'll' works fine at home without any problem in my macos emacs. (ll is aliased to ls -l in my .bashrc). I'd need to know which command you are actually trying to run.
    – freakhill
    Dec 11, 2014 at 17:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.