Mac OS X - Passing pathname with spaces as arguments to bashscript and then issue open Terminal command

霸气de小男生 提交于 2019-11-30 19:55:54

问题


Problem Using bash shell on Mac OS X Mavericks

I created a bash script called test.sh that accepts one command line argument:-

$ cat test.sh
#!/bin/bash
open -a Terminal $1

When i execute this script in the following way:-

$ ./test.sh /Users/myusername/Desktop/folderwithoutspaces/

it executes perfectly and launches a new Terminal window in the given folder.

But when i execute this script in the following way:-

$ ./test.sh /Users/myusername/Desktop/folder\ withspaces/

it fails to open a new Terminal window and shows the following error message:-

The files /Users/myusername/Desktop/folder and /Users/myusername/Desktop/withspaces do not exist.

I tried all these possible ways but wasn't successful at any:-

$ ./test.sh "/Users/myusername/Desktop/folder\ withspaces/"
$ ./test.sh "/Users/myusername/Desktop/folder withspaces/"
$ ./test.sh '/Users/myusername/Desktop/folder\ withspaces/'
$ ./test.sh '/Users/myusername/Desktop/folder withspaces/'
$ ./test.sh /Users/myusername/Desktop/folder\ withspaces/

回答1:


When you use the variable with quoting, it gets split based on IFS (which includes a whitespace by default). The solution is to quote the variable. Instead of saying:

open -a Terminal $1

say:

open -a Terminal "$1"


来源:https://stackoverflow.com/questions/21496182/mac-os-x-passing-pathname-with-spaces-as-arguments-to-bashscript-and-then-issu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!