Search code examples
bashbash-function

How can I check if my Bash function got an argument or not?


Quick question. I love emacs. I hate typing stuff though, so I like to use e to invoke emacs.

Previously I had my .bash_profile (OS X) configured as: alias e="emacs .". However, I was tired of still having to type emacs {file} when I just wanted to edit one file.

So I tried to whip this up, from some googling, but bash is complaining about the []:

###smart emacs, open file, or if none, dir 
e()
{
    if [$1]; then
        emacs $1
    else
        emacs .
    fi
}

I want to use this to do: e something.c or, just e.


Solution

  • #!/bin/bash                       
    
    ###smart emacs, open file, or if none, dir
    e()
    {
        if [[ -z "$1" ]]; then # if "$1" is empty
            emacs .
        else
            emacs "$1"
        fi
    }