Here is my script:
#!/bin/bash echo 'Digite o local em que deseja instalar o IGRAFU(pressione enter para instalar em ${HOME}/IGRAFO):' read caminho if test -z $caminho then caminho='${HOME}/IGRAFO' fi echo 'O IGRAFU será instalado no diretório: $caminho' mkdir -pv $caminho mv -v ./* $caminho echo 'Pronto!'
At ‘read caminho’ I may receive from the user a path like ~/somefolder. When the script receives that kind of path both mv and mkdir won’t make tilde expansion, so it will try to create a ~/somefolder and not /home/username/somefolder and therefore fail.
How do I ensure that the tilde will be converted into the HOME variable?
You will probably need to eval the variable to have it substituted correctly. One example would be to simply do
Keep in mind that this will break if
caminhocontains semicolons or quotes, it will also treat backslashes as escaping, and if the data is untrusted, you need to take care that you’re not the target of an injection attack.Hope that helps.