To customize my bash user experience, is there a way to use .bashrc to recall common command sets. For example I found this set of commands to find a search string in text files,

find /directory -type f -print0 | xargs -0 grep -i 'search string'

I'd like to be able to recall this with a custom alias like `grepfindfile and then view the above command set displayed in bash the same way the last command is pulled from history.

Or would I have to create some sort of bash script? Like bashrc files, do command line users share their productivity scripts? What are they called? My favorite bash widgets?


CF. Same type of problem asked here for VIM How do I create a shortcut for a find and replace command in vim?


You could create a function in .bashrc like this:

searchString ()
  if [ "$1" ] ; then
    find /directory -type f -print0 | xargs -0 grep -i "$1"
    echo "usage ..."
|improve this answer|||||

I'd vote for the old-fashioned way: Create a bin-directory in your home directory and add it to PATH (some distributions will do this by default if the find a ~/bin upon login). Then create any scripts you may find useful in there.


  1. Aliases and shell functions are nice to have, but often you only have them in the Shell itself. You can't call them from other programs (like xargs) or shells. Not so with script files. You may call them like any other UNIX command without restrictions. If coded with #!/bin/usedshell the current shell does not matter and they may be called from any other shell, too.

  2. If one alters login scripts like .*profile and .*shrc one has always to perform a login on a second screen to verify that a typo will not prevent successful logins (annoying).

  3. If I have to work in other user-environments I have my scripts immediately available by just appending my bin-directory to PATH without having to modify other environment settings.

Additional note for beginners:

Don't create too much convenience scripts or aliases for really simple tasks. There will come the time where it is more difficult to remember all your own scripts than it is to remember the unix commands they're based on :-)

|improve this answer|||||
  • +1 for the same thought I had about remembering my own function vars vs learning the commands themselves. Which points to my intention to implement which ever solution helps me recall and memorize commands with max power! – xtian Oct 20 '11 at 18:53

I usually put such commands in bash scripts in my ~/bin directory. I like using scripts better than functions because a script is immediately visible to all my open shells.

For commands that I don't use that often, or that I need to modify each time, I have a file of command lines that I search using grep, then just copy and paste from the screen to the command line.

I also keep a very large bash history file so that commands are available from history for at least a week after I've used them. If I find that I keep recalling the same command often, I put it in a script.

I just discovered this solution elsewhere in superuser that uses the bash history -s command to add commands to your bash history. That solution is probably closest to your original request.

|improve this answer|||||
  • +1 for blindingly obvious advise to use grep on a file of command sets I've already collected. – xtian Oct 20 '11 at 18:50

Start by shortening it to

grep -ir 'search string' /directory

The -r option is for recursive mode.


In general, there are three ways to shorten commands.

  1. Aliases: very basic, only allow substitution of a command. Can be put in ~/.bashrc:

    alias ll="ls -lFh"
    alias md="mkdir"

    would expand ll ~/music to ls -lFh ~/music.

    Aliases have advantages, too – they can be expanded inline; you just need to configure a keyboard shortcut for this. For example, put the following in your ~/.inputrc:

    "\e[1;3C": alias-expand-line

    This corresponds to the key AltRight →.

  2. For something more complex (multiple commands, arguments) – functions allow using the standard syntax features; also in ~/.bashrc:

    count() {
        sort "$@" | uniq -c | sort -n -r

    These can include multiple commands, set variables, have conditions... anything that you could do in a shell script. They, however, cannot be inline-expanded.

  3. External scripts – almost same as functions, but without the foo() { ... } and in a separate file. (~/bin is a popular location.)

    post() { curl -sF "sprunge=<$1" http://sprunge.us/; }
    if [ -z "$1" ]; then
            file=$(mktemp /tmp/sprunge.XXXXXXXX)
            cat > "$file"
            if [ -s "$file" ]; then
                    post "$file"
                    echo "stdin empty" >&2
            rm -f "$file"
            for file; do
                    echo -n "$file →"
                    post "$file"

    The advantage of putting scripts in separate files is one: They are not limited to bash features. There have been many cases where I started with a bash script, then rewrote it in Python, later it became Perl...

|improve this answer|||||
  • +1 For the comprehensive overview. This goes the distance to confirm that I will continue to resist bash scripting for the time being, if only because I find if[]; then else fi to be weird to my PHP mind. – xtian Oct 20 '11 at 18:55
  • Fine, just write PHP then. #!/usr/bin/env php... – user1686 Oct 21 '11 at 12:51
  • Hey, You're blowing my mind. I can use PHP with bash commands? And, print/echo to prompt? – xtian Oct 21 '11 at 21:46
  • As long as you have the CLI version installed, yes. Example. But it's somewhat limited. – user1686 Oct 22 '11 at 11:20
  • 1
    Oh, and do please note grep -r as well as find ... -exec instead of that unnecessary xargs. – user1686 Oct 22 '11 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.