Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

So I'm messing around in my .bashrc making different aliases and playing with my prompt. I'm trying now to make an alias that will move into a specific directory, make a new directory based on the date, and make a new file. All in one alias. Here's what I've done...

export DATE="$( date +%d-%b )"
alias hw='cd ~/Java/Homework/257; mkdir $DATE; cd $DATE; vim'

Now if I go out and run hw Client.java it should move into ~/Java/Homework/257/27-Jan/ and open a new file called Client.java in vim. It all works beautifully except the date variable doesn't work right. It moves me into the ~/Java/Homework/257 directory and makes two directories. One called date and another called +%d-%b and puts the Client.java file in the date directory.

I thought my date variable export simply wasn't working but when I type $DATE at the command prompt it gives me 27-Jan like it should. So does anyone know what could actually be causing this? A friend of mine did something very simliar to this using a slightly different format string for the date and his works just fine. The only thing I have different on my computer is bash-completion from homebrew but I don't see how that would effect my date...

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted
export DATE="$( date +%d-%b )"

You need to actually execute it. Otherwise you just assign a string value. Since you're missing quotes around the $DATE, it will be interpreted as two separate arguments, date and +%d-%b.

share|improve this answer
    
This "works" in a way. It fixed the date issue, my problem now is that it isn't doing everything else in the alias first. It's supposed to change directories into my homework directory before it makes the date directory and opens vim. –  CaldwellYSR Jan 28 '12 at 21:15
    
Nevermind I figured out the problem! I messed up the path to the homework directory. –  CaldwellYSR Jan 28 '12 at 21:19
    
@CaldwellYSR Glad my answer helped. –  Daniel Beck Jan 28 '12 at 21:26
2  
@CaldwellYSR: If you use say cd wherever && mkdir "$DATE" && cd "$DATE" && vim (i.e. use && instead of ;), it will be easier to notice when one of those middle commands fails (the rest will not run and the failing one (the last one run) will probably have written an error message). –  Chris Johnsen Jan 29 '12 at 3:34
    
learn something new everyday. Thanks! –  CaldwellYSR Jan 29 '12 at 14:49
add comment

Try to define go as a function in .bashrc or .alias:

go() {
    DATE=$(date +%d-%b)
    mkdir "$DATE"
    cd "$DATE"
    vim "$1"
}

This ensures that the date is determined every time you use the command and not just when you login/start bash.

share|improve this answer
    
That had the unfortunate side effect of infinite loopage somehow and now I have 17million directories deep of 27-jan XD –  CaldwellYSR Jan 27 '12 at 7:57
    
How did you call the function? –  patrix Jan 27 '12 at 8:23
    
go()... is that not how to call it? –  CaldwellYSR Jan 27 '12 at 14:54
    
Just typing go is enough –  patrix Jan 27 '12 at 16:19
add comment

Along the lines of the other answers: you need to execute the date command. Probably the simplest solution would be to change your quotes to back ticks:

export DATE=`date +%d-%b`
share|improve this answer
    
OH They are back ticks... I thought it was single quotes. That's what I was messing up!!! –  CaldwellYSR Jan 27 '12 at 20:40
    
In the alias statements you need quotes, as you are just replacing, for example, go with that literal string. When you want to execute code, you can place it in back ticks or use the $(...) formalism mentioned in the other answers. –  cm2 Jan 27 '12 at 20:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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