One-liner Bash Functions

September 16, 2014

Here’s a problem:

$ grep something file

If you’re going to search for different strings, repeatedly, what you really want is for “something” to be the last argument. This is true even if you master the keyboard shortcuts to minimize the pain.

While bash doesn’t support anonymous functions, it’s casual about aliases and named functions. It’s easy to create a function in the current session, and it will disappear when you exit the shell. Here’s a bash function to solve our argument problem:

grep_file() {
  grep "$@" file

But it’s not convenient to define a bash function like this: as you press enter between the lines, you can’t go back and edit the top of the function. You could type the function in a file and source it, but it breaks the flow of what you’re doing.

If you’ve ever tried to type the function on one line, you’ve seen this situation:

$ grep_file() { grep "$@" file }

You’re stuck on the next line, and bash is waiting for input … as if you had forgotten to close a string or a parenthesis.

The solution is explained on this page:

one line bash function

It’s all about that semicolon:

$ grep_file() { grep "$@" file; }

Discuss on Twitter