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 *'