Ubuntu – Variable is not found in a bash script

bashscripts

I have written the following code to create a directory where the directory name will look like "nowt_hour_nimute_second". But the code is failing in Bash Shell.

#THIS CODE WILL CREATE A DIRECTORY WITH TIME OF CREATION AS PART OF DIRECTORY NAME
#AUTHOR: SUBIR ADHIKARI
#DATE: 02/12/2014

echo "The time is $(date +%H_%M_%S)"
now=$(date +%H_%M_%S)
echo $now
echo $(pwd)
createdep=nowt_$now
echo $(createdep)
mkdir createdep

On executing, I am getting the following output…

The time is 01_12_30
01_12_30
/home/adhikarisubir/test/basic_unix
createfiles.sh: 10: createfiles.sh: createdep: not found

What am I missing here?

Best Answer

Just like the error says: createdep is not a program.

Change this:

echo $(createdep)
mkdir createdep

to this:

echo "$createdep"
mkdir "$createdep"

Note that the format string for date can contain regular characters too, so you don't need the "now" variable:

createdep=$(date +"nowt_%H_%M_%S")
Related Question