I'm trying to achieve something really simple here, but something is missing.
I'm trying to create a file via bash script (.sh).
When I try direct on my terminal:
touch /Users/luco/Downloads/My\ Test\ Folder/test.txt
.../test.txt: No such file or directory
./Script.sh "/Users/luco/Downloads/My\ Test\ Folder/"
./Script.sh /Users/luco/Downloads/My\ Test\ Folder/
Using both quotes and backslashes at invocation time will produce the same error, using either this:
#!/bin/sh touch "$1/test.txt"
#!/bin/sh touch "$1"/test.txt
The problem can be reproduced as such:
$ ./test.sh "/Users/admin/Developmemt/Pippo\ Pelo" touch: /Users/admin/Developmemt/Pippo\ Pelo/test.txt: No such file or directory
I get the error (due to the backslash being inside quotes --
"/" -- thus meaning that a literal backslash is expected to be part of the directory name).
By contrast, if not using quotes, the backslash is read as a signal to the shell, not as a part of the directory name, so it works:
$ ./test.sh /Users/admin/Developmemt/Pippo\ Pelo
A solution comes using
sed to handle the
#!/bin/sh path=$(echo "$1" | sed 's/\\//g') touch "$path/test.txt"
so it works in both cases: with or without quotes
\ in path:
admin@macbookproloreto:~/Developmemt/Pippo Pelo$ ./test.sh "/Users/admin/Developmemt/Pippo\ Pelo" admin@macbookproloreto:~/Developmemt/Pippo Pelo$ ./test.sh /Users/admin/Developmemt/Pippo\ Pelo