This question already has an answer here:

I am trying to create an alias in .bashrc to time-stamp a directory. Obviously, it's not working out too well for me. For example:

under .bashrc

alias testit="export testor=$(date +%d);mkdir ~/Desktop/$testor"


~> testit
mkdir: cannot create directory ‘/home/SJL/Desktop/’: File exists

now if I run in command instead

~> export testor=$(date +%d)
~> mkdir ~/Desktop/$testor

directory created, no problem.

Now if I run instead:

~> export testor=$(date +%d);mkdir ~/Desktop/$testor

again, no problem.

going back to using the alias stated in .bashrc it does not work.

It's really curious because I've done similar things many many many times, but it seems that there's something going on/wrong with the date formatting.

I would really appreciate your input.

marked as duplicate by Michael Homer, Satō Katsura, phk, Kusalananda, Gilles May 13 '17 at 22:29

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


When you create the alias, $testor has no value, and so the alias will be

export testor=13;mkdir ~/Desktop/

You can see what an alias expands to with alias aliasname.

To fix this, use single quotes instead of double quotes. This defers the expansion of variables and command substitutions until you actually invoke the alias.

If you don't need $testor for anything else, just use

alias tester='mkdir "$HOME/Desktop/$(date '+%d')"'

Reading your comments, this would also work:

alias makedir='mkdir "$HOME/Desktop/$(date '+%d')"'
alias copyfile='cp somefile "$HOME/Desktop/$(date '+%d')"

Or, alternatively, a more elaborate shell function that allows for copying any file to the folder (which you would never be able to do with an alias).

copyfile () {
    destfolder="$HOME/Desktop/$(date '+%d')"
    if [ -d "$destfolder" ]; then
        mkdir "$destfolder"
    cp "$1" "$destfolder"

This defines a function called copyfile that will use cp to copy the file given on the command line to a date-stamped folder. If the folder does not exist, it is created.

  • Thank you, I appreciate the input, but I do need a dynamic variable. – user231192 May 13 '17 at 8:31
  • @user231192 So go with my suggestion and just replace the double quotes with single quotes then. – Kusalananda May 13 '17 at 8:32
  • I just checked it, oh wow... That works! Thanks! – user231192 May 13 '17 at 8:35
  • would you point me to the documents in regards to single and double quotes please? – user231192 May 13 '17 at 8:36
  • @user231192: Look in the bash man page in the section QUOTING. – camh May 13 '17 at 8:48

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