3

I want to launch emacs with gdb running. I'd like to have all of the command line arguments fed in at the same time. This post: Start emacs-gdb from command line?, already solves 95% of the problem, but I have an annoying nagging follow-on to the problem. I really need to be able to pass in arguments that have spaces, something ala

edbg path/to/myprog --firstarg "something with spaces" --secondarg 1

I've been searching on this site and on google, and I played around for a couple of hours, but I can't seem to figure this out.

Any ideas?

1 Answer 1

3

So, as I understand it you have this Bash function defined:

edbg() { emacs --eval "(gdb \"gdb --annotate=3 $*\")";}

and it doesn't work because any spaces in the arguments get reparsed into new word boundaries. Well, this is a common enough problem in Bash scripts that there's a special variable $@ that expands the arguments to the function differently when it's inside a double-quoted string. This gets us half-way there. The rest is just putting the quotes back around them:

edbg() {
  arglist="";
  for a in "$@"; do
    if [[ $a == ${a/ /} ]]; then
      arglist="$arglist $a";
    else
      arglist="$arglist \\\"$a\\\"";
    fi
  done;
  emacs --eval "(gdb \"gdb --annotate $arglist\")"
}

Note that this won't put quotes around args that contain tabs or newlines which would also require quotes; your $IFS may vary.

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

1 Comment

Thanks db48x. This took me where I needed to go. To help others, if you patch up the above with the following line, the example should completely work as expected: emacs --eval "(gdb \"gdb --annotate=3; -cd pwd --args $arglist\")"

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.