date - How to pass a variable to the mv command to rename a file text with spaces and the variable's text in a bash (.sh) file -


i create variable , store day, date & time in it:

now=$(date "+%a %d/%m/%y% %h:%m") 

then pass $now mv command rename file.

e.g. create file named a.txt title , current date:

printf "file report (" > ~/desktop/a.txt echo $now"):\n" >> ~/desktop/a.txt   

then try rename file variable ($now) included in name:

mv ~/desktop/a.txt ~/desktop/'file report $now'.txt 

what should last line be? tried these 2 options.

mv ~/desktop/a.txt ~/desktop/'file report' $now.txt 

&

mv ~/desktop/a.txt ~/desktop/'file report'${now}.txt 

assuming reasonably bourne-like shell (such bash), variable substitution not happen inside single quotes. need use double quotes:

mv ~/desktop/a.txt "${home}/desktop/file report ${now}.txt" 

(i'm not sure whether curly braces required, can't hurt)

you need change date command avoid use of slashes. example:

now="$(date '+%a %d-%m-%y% %h:%m')" 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -