first possibility
credit @glenn jackman
The escape sequence \
is not defined within quotes by Bash, so it expands to \
(the backslash is preserved). To see this:
echo "foo\ bar"
-> foo\ bar
echo foo\ bar
-> foo bar
It's possible, then, that some peculiar quirk with the MacOS terminal leads to the difference in behaviour.
second possibility
When using the script, you need to keep in mind the current working directory. Paths in Bash scripts are resolved relative to the CWD of the shell (process) that invoked the script.
For example, say we have the following file hierarchy, and script.sh
contains cat bar/qux.txt
.
foo
├── bar
│ └── qux.txt
└── script.sh
Then
- Invoking
script.sh
from the foo
directory will work (the contents of qux.txt
will be printed)
- Invoking
script.sh
from anywhere else will not work
To fix this, you can either use a clever Bash expression to figure out the path to the script, and look for bar/qux.txt
relative to that, or you can specify the path to qux.txt
using a rooted path (e.g. /home/me/...
)
mv "dir\ 1/file1.txt" "dir\ 1/file2.txt"
in a "Terminal" and it succeeded at renaming a file calledfile1.txt
in a directory calleddir 1
(and notdir\ 1
)? Can you reproduce it? What application was running at the time inside your "Terminal"? Was it a shell? Which one? I don't know of any shell where that would work. Certainly not bash nor zsh (the default login shell on macos these days I believe)