1

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...

3 Answers 3

2
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.

4
  • 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. Commented Jan 28, 2012 at 21:15
  • Nevermind I figured out the problem! I messed up the path to the homework directory. Commented Jan 28, 2012 at 21:19
  • @CaldwellYSR Glad my answer helped. Commented Jan 28, 2012 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). Commented Jan 29, 2012 at 3:34
2

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.

4
  • That had the unfortunate side effect of infinite loopage somehow and now I have 17million directories deep of 27-jan XD Commented Jan 27, 2012 at 7:57
  • How did you call the function?
    – nohillside
    Commented Jan 27, 2012 at 8:23
  • go()... is that not how to call it? Commented Jan 27, 2012 at 14:54
  • Just typing go is enough
    – nohillside
    Commented Jan 27, 2012 at 16:19
1

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`
2
  • OH They are back ticks... I thought it was single quotes. That's what I was messing up!!! Commented Jan 27, 2012 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
    Commented Jan 27, 2012 at 20:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .