How does unix handle full path name with space and arguments ?
In windows we quote the path and add the command-line arguments after, how is it in unix?
unix如何处理带有空格和参数的完整路径名?在windows中我们引用路径并添加命令行参数,如何在unix中?
"c:\foo folder with space\foo.exe" -help
update:
I meant how do I recognize a path from the command line arguments.
我的意思是如何从命令行参数中识别路径。
7 个解决方案
#1
37
You can either quote it like your Windows example above, or escape the spaces with backslashes:
您可以像上面的Windows示例一样引用它,也可以使用反斜杠转义空格:
"/foo folder with space/foo" --help
/foo\ folder\ with\ space/foo --help
#2
9
You can quote if you like, or you can escape the spaces with a preceding \, but most UNIX paths (Mac OS X aside) don't have spaces in them.
如果您愿意,可以引用,或者您可以使用前面的\来转义空格,但大多数UNIX路径(除了Mac OS X)都没有空格。
/Applications/Image\ Capture.app/Contents/MacOS/Image\ Capture
"/Applications/Image Capture.app/Contents/MacOS/Image Capture"
/Applications/"Image Capture.app"/Contents/MacOS/"Image Capture"
All refer to the same executable under Mac OS X.
所有都是指Mac OS X下的相同可执行文件。
I'm not sure what you mean about recognizing a path - if any of the above paths are passed as a parameter to a program the shell will put the entire string in one variable - you don't have to parse multiple arguments to get the entire path.
我不确定你对识别路径的意思 - 如果上述任何路径作为参数传递给程序,shell会将整个字符串放在一个变量中 - 你不必解析多个参数来获取整条路。
#3
3
Since spaces are used to separate command line arguments, they have to be escaped from the shell. This can be done with either a backslash () or quotes:
由于空格用于分隔命令行参数,因此必须从shell中转义它们。这可以使用反斜杠()或引号来完成:
"/path/with/spaces in it/to/a/file"
somecommand -spaced\ option
somecommand "-spaced option"
somecommand '-spaced option'
This is assuming you're running from a shell. If you're writing code, you can usually pass the arguments directly, avoiding the problem:
这假设你是从shell运行的。如果您正在编写代码,通常可以直接传递参数,避免出现问题:
Example in perl. Instead of doing:
perl中的示例。而不是做:
print("code sample");
system("somecommand -spaced option");
print(“code sample”); system(“somecommand -spaced option”);
you can do
你可以做
print("code sample");
system("somecommand", "-spaced option");
print(“code sample”); system(“somecommand”,“ - spaced option”);
Since when you pass the system() call a list, it doesn't break arguments on spaces like it does with a single argument call.
因为当你传递system()调用一个列表时,它不会破坏空格上的参数,就像它使用单个参数调用一样。
#4
1
You can quote the entire path as in windows or you can escape the spaces like in:
您可以在窗口中引用整个路径,也可以在以下位置转义空格:
/foo\ folder\ with\ space/foo.sh -help
Both ways will work!
两种方式都有效!
#5
1
Also be careful with double-quotes -- on the Unix shell this expands variables. Some are obvious (like $foo
and \t
) but some are not (like !foo
).
还要注意双引号 - 在Unix shell上,这会扩展变量。有些是显而易见的(比如$ foo和\ t)但有些则不是(比如!foo)。
For safety, use single-quotes!
为安全起见,请使用单引号!
#6
0
If the normal ways don't work, trying substituting spaces with %20
.
如果正常方法不起作用,请尝试用%20替换空格。
This worked for me when dealing with SSH and other domain-style commands like auto_smb
.
在处理SSH和其他域式命令(如auto_smb)时,这对我有用。
#7
0
I would also like to point out that in case you are using command line arguments as part of a shell script (.sh file), then within the script, you would need to enclose the argument in quotes. So if your command looks like
我还想指出,如果您使用命令行参数作为shell脚本(.sh文件)的一部分,那么在脚本中,您需要将参数括在引号中。所以如果你的命令看起来像
>scriptName.sh arg1 arg2
And arg1 is your path that has spaces, then within the shell script, you would need to refer to it as "$arg1" instead of $arg1
而arg1是你的路径,有空格,然后在shell脚本中,你需要将它称为“$ arg1”而不是$ arg1
这是细节
#1
37
You can either quote it like your Windows example above, or escape the spaces with backslashes:
您可以像上面的Windows示例一样引用它,也可以使用反斜杠转义空格:
"/foo folder with space/foo" --help
/foo\ folder\ with\ space/foo --help
#2
9
You can quote if you like, or you can escape the spaces with a preceding \, but most UNIX paths (Mac OS X aside) don't have spaces in them.
如果您愿意,可以引用,或者您可以使用前面的\来转义空格,但大多数UNIX路径(除了Mac OS X)都没有空格。
/Applications/Image\ Capture.app/Contents/MacOS/Image\ Capture
"/Applications/Image Capture.app/Contents/MacOS/Image Capture"
/Applications/"Image Capture.app"/Contents/MacOS/"Image Capture"
All refer to the same executable under Mac OS X.
所有都是指Mac OS X下的相同可执行文件。
I'm not sure what you mean about recognizing a path - if any of the above paths are passed as a parameter to a program the shell will put the entire string in one variable - you don't have to parse multiple arguments to get the entire path.
我不确定你对识别路径的意思 - 如果上述任何路径作为参数传递给程序,shell会将整个字符串放在一个变量中 - 你不必解析多个参数来获取整条路。
#3
3
Since spaces are used to separate command line arguments, they have to be escaped from the shell. This can be done with either a backslash () or quotes:
由于空格用于分隔命令行参数,因此必须从shell中转义它们。这可以使用反斜杠()或引号来完成:
"/path/with/spaces in it/to/a/file"
somecommand -spaced\ option
somecommand "-spaced option"
somecommand '-spaced option'
This is assuming you're running from a shell. If you're writing code, you can usually pass the arguments directly, avoiding the problem:
这假设你是从shell运行的。如果您正在编写代码,通常可以直接传递参数,避免出现问题:
Example in perl. Instead of doing:
perl中的示例。而不是做:
print("code sample");
system("somecommand -spaced option");
print(“code sample”); system(“somecommand -spaced option”);
you can do
你可以做
print("code sample");
system("somecommand", "-spaced option");
print(“code sample”); system(“somecommand”,“ - spaced option”);
Since when you pass the system() call a list, it doesn't break arguments on spaces like it does with a single argument call.
因为当你传递system()调用一个列表时,它不会破坏空格上的参数,就像它使用单个参数调用一样。
#4
1
You can quote the entire path as in windows or you can escape the spaces like in:
您可以在窗口中引用整个路径,也可以在以下位置转义空格:
/foo\ folder\ with\ space/foo.sh -help
Both ways will work!
两种方式都有效!
#5
1
Also be careful with double-quotes -- on the Unix shell this expands variables. Some are obvious (like $foo
and \t
) but some are not (like !foo
).
还要注意双引号 - 在Unix shell上,这会扩展变量。有些是显而易见的(比如$ foo和\ t)但有些则不是(比如!foo)。
For safety, use single-quotes!
为安全起见,请使用单引号!
#6
0
If the normal ways don't work, trying substituting spaces with %20
.
如果正常方法不起作用,请尝试用%20替换空格。
This worked for me when dealing with SSH and other domain-style commands like auto_smb
.
在处理SSH和其他域式命令(如auto_smb)时,这对我有用。
#7
0
I would also like to point out that in case you are using command line arguments as part of a shell script (.sh file), then within the script, you would need to enclose the argument in quotes. So if your command looks like
我还想指出,如果您使用命令行参数作为shell脚本(.sh文件)的一部分,那么在脚本中,您需要将参数括在引号中。所以如果你的命令看起来像
>scriptName.sh arg1 arg2
And arg1 is your path that has spaces, then within the shell script, you would need to refer to it as "$arg1" instead of $arg1
而arg1是你的路径,有空格,然后在shell脚本中,你需要将它称为“$ arg1”而不是$ arg1
这是细节