1

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?

1 Answer 1

3

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")
4
  • It would be very helpful of you if you can just show the difference between echo $(createdep) and echo "$createdep". I was under the impression in case of echo " is not necessary.
    – Mistu4u
    Dec 1, 2014 at 20:13
  • $(createdep) starts a subshell and attempts to run the program named "createdep" (see command substitution in the manual). The difference between quotes and no quotes is that quotes will prevent word splitting and filename expansion... Dec 1, 2014 at 23:55
  • If $createdep contains "foo<tab>bar", then echo $createdep (no quotes) will only display a single space between foo and bar. If $createdep contains "*", then echo $createdep (no quotes) will display all the filenames in the current directory Dec 1, 2014 at 23:56
  • You may be thinking of echo ${createdeps} (with curly braces). That is not the same as using double quotes. As the manual says: "The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name." Dec 1, 2014 at 23:58

You must log in to answer this question.

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