Reputation: 37
In MacOSX it is possible to create a folder in Finder that contains a forward slash "/" in the name.
I am trying to bash script the same by doing something similar to
args=("$@")
name=${args[0]}
street_corner=${args[1]}
my_folder="Hello ${name} - ${streetcorner}"
mk () {
case "$1" in /*) :;; *) set -- "./$1";; esac
mkdir -p "$1"
}
mk $my_folder
where street corner is a crossing in the format "street 1/street 2" and thus needs the slash to be kept in the directory name.
I read several articles like this one where they explain how to manipulate the string to escape or keep the forward when you set the string yourself. The problem I have is that the user passes the argument to be used in the script and therefore the string is already encapsulated when i run the command to the make a directory with $my_folder as argument.
How do I expand the value of street_corner without letting the forward slash create a subdirectory named "street 2" located inside the directory "street 1" but instead creating a single directory called "street 1/street 2"?
I have tried these fixes and failed
street_corner="${args[0]}"
my_folder='"Hello ${name} - ${streetcorner}"'
my_folder="'Hello ${name} - ${streetcorner}'"
my_folder="Hello ${name} - ${'streetcorner'}"
my_folder="Hello ${name} - ${$"streetcorner"}"
my_folder="Hello "${name}" - "${streetcorner}
And also tried to give the input for street_corner like
./my_script.sh "name" "street 1\/street 2"
./my_script.sh "name" 'street 1/street 2'
./my_script.sh "name" "'street 1/street 2'"
Upvotes: 1
Views: 2757
Reputation: 531135
Path names involving /
and :
(for reasons to be discussed below) are a bit confusing under macOS. The short answer is:
/
in a file name.:
.First, /
is not a valid character in a POSIX path name; it is reserved as the path component separator. Command line tools that expect valid file names as arguments will reject those containing /
.
The Finder appears to let you create a file or folder whose name contains a /
, but it silently converts the /
to a :
. You can confirm this from the terminal. Create a folder named foo/bar
in your home directory; it will appear as foo:bar
if you run ls
from the terminal.
Second, :
was historically used as the path separator in classic Mac OS. The Finder still does not allow you to create a folder whose name contains a :
, but command line tools (like mkdir
) do. mkdir foo:bar
will work, and further the name will appear as foo/bar
in the Finder.
Upvotes: 5
Reputation: 6995
Your issue is not one of quoting.
The forward slash is the path separator in UNIX-type operating systems. It is pretty much the only character you cannot have in a file name (although it can be part of the path, where it allows the directory hierarchy to be expressed). You can have spaces, tabs, even newlines. But no forward slashes.
So the answer is, you cannot use street1/street2
as a filename without street1
being interpreted as anything other than a directory.
Upvotes: 1
Reputation: 1273
Inside the script, you need to double quote the right hand side of the lines:
name="${args[0]}"
street_corner="${args[1]}"
and the command:
mk "$my_folder"
Also there is a typo. Remove the underscore from street_corner.
Then call the script like this:
./my_script.sh "name" "street 1\/street 2"
or
./my_script.sh "name" 'street 1/street 2'
Upvotes: 0