I'm still getting around the quirks of shell scripting, but I feel like I'm getting a little more comfortable with it.

I've been trying to create an alias to generate a tar file that I have to create frequently, but it always gives me tar errors. Just as a test I tried to create a simple wrapper but it doesn't work either:

function eztar(){

    if [ -d $1 ]
        export QTAR="$1.tar";
        tar –cvf ${QTAR} "$1/";

        echo "Missing directory name";


>eztar mydir
tar: invalid option -- ''
Try `tar --help' or `tar --usage' for more information.

Is the -cvf option in this case getting eaten up somehow?

Anyway here's another one.

I had a problem where Apache would run out of memory. I found out that too many failed shutdowns caused the semaphore to get full, so to fix that I discovered a script I liked and I tried to get it to run in alias. (BTW is there a better way to do this?) I think I need to put this in a function because when I check the aliases the for loop had expanded to the list of current semaphores, which isn't what I had intended.

alias semfix='eval "for i in `ipcs -s # grep apache # awk '{print $2}'` ; do ipcrm -s $i; done";'

I'm learning I can't just create an alias or wrapper function out of everything on Unix!

2 Answers 2


There's nothing wrong with your first function except there's a typo.
The character in front of the cvf isn't a minus char.
Are you using a std. US 101 key keyboard?

Taking that out altogether (the minus is optional on most tar switches) and the function works fine.
Or else, you could fix it with a proper -cvf

Also, there's no need for the export
export causes an environment variable to have more persistence than you need in a function like this.

I suggest you read the Advanced Bash-Scripting Guide
Look at let, local and export.
See how they vary, when to use one or the other.

Yes, you should try and rewrite the second as a function.
The less nested quoting and piping, while learning, will make writing functions and aliases easier.

The one liners come later :)


Try using tar without the minus sign, i.e. just tar cfv

It's the original, now non-standard format.

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.