0

I'm trying to write a bash function or alias which passes a shell command to a remote API.

Specifically, this is what I have so far in .bashrc:

explain () {
  cmd=$(printf "%q " "$@")
  curl -Gs "https://www.mankier.com/api/explain/?format=text&cols="$(tput cols) --data-urlencode "q=$cmd"
}
export -f explain

I can use that with something like:

$ explain ls -lh

But I haven't found a way to handle commands with parameter expansion. A couple examples:

$ explain ls -lh $HOME

$HOME is changed to /home/j, but I would like to pass the string "ls -lh $HOME" to the remote API.

$ explain ls -lh *

* is changed to the directory contents, but I would like to pass the string "ls -lh *" to the remote API.

Is this possible to do without escaping the command I want to explain? I.e. changes within the explain function, not: explain 'ls -lh *'

Jackson Pauls
  • 225
  • 2
  • 12
  • Good news: You can see my [answer here](http://stackoverflow.com/a/14966731/1501222) for a way to access the unescaped command (their for no file expansion and no parentheses evaluation) . Bad news: You do not want to do that – BeniBela Sep 19 '14 at 20:09
  • Thanks very much @BeniBela - could you mark this question as a duplicate of the one you linked to? (I don't have enough reputation.) The requirements are effectively the same. – Jackson Pauls Sep 22 '14 at 12:49
  • seems I can vote for that (never done that before..): possible duplicate of [minimal typing command line calculator - tcsh vs bash](http://stackoverflow.com/questions/14966102/minimal-typing-command-line-calculator-tcsh-vs-bash) – BeniBela Sep 22 '14 at 12:55

1 Answers1

0

Double quotes interpolate shell variables, as you are seeing here with $HOME being replaced by its value. Use single quotes to prevent interpolation, explain 'ls -lh *'.

Wm Annis
  • 123
  • 4
  • Good shout, thanks. But I was really looking for a way to achieve this without the quoting, so I could e.g. run `explain !!` to explain the last command. I'll amend the question to make that clear. I realise this might not be possible by the way, and am prepared to accept no as an answer. – Jackson Pauls Sep 19 '14 at 19:10