How to pass filename with accentuation and spaces as parameter for shell script file

Asked

Viewed 1,956 times

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 Linux - Ubuntu

  • How are you reading the parameters? You are not using the variables: $N (N > 0) to get the parameters?

  • the reading is correct, I take $1 and put in a variable. When it goes without space it works right.

  • at the beginning of the script IFS=$'\n and in the end unset IFS

  • It works if you put double quotes around the variable? "$@" or "$1"?

  • 2

    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...

  • 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 '

  • 3

    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.

Show 4 more comments

1 answer

0


The exit was to use double quotes like Wakim and Antonio mentioned, so I’m marking the issue as resolved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.