4

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.

7
  • 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
    Commented Dec 11, 2014 at 15:02
  • @choroba Yes it works if adding 2>/dev/null.. Thanks! But why is it working? Commented 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) Commented Dec 11, 2014 at 17:16
  • Where exactly did you add it?
    – choroba
    Commented Dec 11, 2014 at 17:16
  • 1
    I tried (print (shell-command-to-string "bash -ic 'my_cmd' 2>/dev/null") (current-buffer)).
    – choroba
    Commented Dec 11, 2014 at 17:49

3 Answers 3

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")
       (current-buffer))

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

3

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
fi

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

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

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."
  (with-output-to-string
    (with-current-buffer
      standard-output
      (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]

edit:

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

14
  • 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
    Commented 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
    Commented 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
    Commented 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 :( Commented 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
    Commented 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.