Search code examples
bashquotesmkdir

basic question: using a variable content as directory name in Linux


I want to make a new directory with the name of every zip file currently exists in a specific directory, in another one. I have written a 'for loop' for this purpose:

files=NL*.zip
for a in $files; do 
b=echo $a; 
sudo mkdir '/home/shokufeh/Desktop/normals/T1/"${b}"';
done

but it creates a directory named ${b} for the first round and make errors for next ones. Could you please tell me what I should do?


Solution

  • You put your variable in simple quotes, there fore '${b}' will never be interpreted. Try this:

    for a in NL*.zip; do 
      sudo mkdir "/home/shokufeh/Desktop/normals/T1/${a}";
    done
    

    No need for variables $files and $b.

    To summarize, let's say var=3,

    • echo "$var" will display 3
    • echo '$var' will display $var
    • echo "'$var'" will display '3'
    • echo '"$var"' will display "$var"

    Hopefully this makes sense. Quotes function like parentheses and brackets.