2
I densenveloped a shell script that gets the location of a file and does several manipulations on it, e.g.: ./processes.sh path/file.txt
However, when I send a file name as a parameter that contains spaces it gives an error because it puts ' ' in each space, and so is interpreted by bash as a directory. Ex: path/filename.txt Output: path/filename.txt
How to prevent this?
What operating system is?
– Tony
@Tony Linux - Ubuntu
– Raul Sena Ferreira
How are you reading the parameters? You are not using the variables: $N (N > 0) to get the parameters?
– Wakim
the reading is correct, I take $1 and put in a variable. When it goes without space it works right.
– Raul Sena Ferreira
at the beginning of the script
IFS=$'\n
and in the endunset IFS
– Lucas Virgili
It works if you put double quotes around the variable?
"$@"
or"$1"
?– Anthony Accioly
Double quotes also on the call:
./processa.sh "path/nome do arquivo.txt"
, else each word separated by space will be considered a different parameter: $1, $2, etc...– Wakim
What @Wakim flw makes sense, so I treat the parameter as a single string, I will test tbm if there is any difference between using " or '
– Raul Sena Ferreira
Actually on the call you can use
\
or double quotes (anyway the system reads everything as a single variable), in using the variable you should use double quotes to escape the spaces. See: http://www.unix.com/os-x-apple-/208819-file-paths-spaces-variable.html for examples.– Anthony Accioly