Skip to content

Latest commit

 

History

History
53 lines (32 loc) · 3.01 KB

passing-arguments-to-a-command-alias.md

File metadata and controls

53 lines (32 loc) · 3.01 KB

Pass arguments to a command alias

Bash aliases are nice shortcuts for long command strings or arcane command flags. For example, here's a pair of alias that I use to get the current time as an ISO8601-like date and time string into the clipboard:

alias fdate='date +"%F %R"'
alias fcdate='echo -n `fdate` | pbcopy'

When I execute the command fcdate at the time of this writing, this string is stored in the clipboard: "2017-02-10 05:14".

But the alias operation is just string expansion. It does no processing on the string and cannot reference arguments provided after the alias.

For example, I have a bash function called "longurl" that can take one or two arguments. It's purpose is to take a shortened URL, or otherwise redirected URL, and report the URL at the end of the chain of redirection, i.e., the "lengthened" URL. (For details, see my blog post: A CLI URL lengthener.)

The function returns an integer return code to indicate success or failure and it stores the lengthened URL in a global variable "longurl" (yep, that's the same name as the function).

To use that function I would have to do this command sequence:

longurl "https://pic.twitter.com/EJFvvD5x3A" && echo "" && echo "$longurl"

If I wanted to pipe the output into pbcopy I'd do:

longurl "https://pic.twitter.com/EJFvvD5x3A" && echo -n "$longurl" | pbcopy

I wanted to simplify that command sequence, to create an alias for it. This is not straightforward because in the middle of the command is the URL, something that will vary each time the alias is invoked. I need to supply the URL as an argument after the alias name. How to do that?

The answer is to specify the alias as a function definition and subsequent invocation. I alias the execution of that function this way:

alias lu='function X_(){ longurl "$@" && echo "" && echo "$longurl"; };X_'
alias luc='function X_(){ longurl "$@" && echo -n "$longurl"; };X_'

Then I can do lu https://pic.twitter.com/EJFvvD5x3A and see that the ultimate URL is https://twitter.com/MachinePix/status/828428492744249344/photo/1.

Or I can resolve the shortened URL and put the result in the clipboard with luc https://pic.twitter.com/EJFvvD5x3A | pbcopy.

Credits

I discovered this technique in this Slashdot answer: http://stackoverflow.com/a/22684652/1392864


© 2017 Dave Hein

Creative Commons License
This work by Dave Hein is licensed under a Creative Commons Attribution 4.0 International License.