Swift应用中chmod =a#命令在bash正常,zsh执行报错的原因?
在Swift中使用zsh执行chmod =a#编辑ACL时的报错问题及解决方法
问题现象
在Swift应用中调用系统Shell执行ACL编辑命令时,出现如下差异:
chmod +a#和chmod -a#命令在bash与zsh环境下均能正常执行chmod =a#命令仅在bash中正常工作,切换到zsh时会触发报错:zsh:1: a# not found
问题原因
zsh存在特殊语法规则:=xxx 会被解析为查找名为xxx的命令的绝对路径(例如执行=ls会返回/bin/ls)。当运行chmod =a# ...时,zsh会优先尝试解析=a#,试图查找名为a#的系统命令,找不到则抛出上述错误;而bash无此语法特性,会直接将=a#作为参数传递给chmod,因此可以正常执行。
解决方案
需要阻止zsh解析=a#的特殊语法,有两种可靠方式:
- 给
=a#包裹单引号,让zsh将其视为纯字符串:'=a#' - 转义
=符号:\=a#
修改后的代码示例
更新编辑ACL的命令字符串即可适配zsh:
// 编辑ACL条目 - 使用单引号包裹适配zsh @State private var editACLentryString: String = "chmod '=a#' 1 \"user:j.doe allow execute,writeattr,writeextattr,delete,delete_child,add_file,add_subdirectory,write,append,writesecurity,chown\" /Volumes/snfs1/pathTofile/5888709220764_p.jpg"
或者采用转义方式:
// 编辑ACL条目 - 转义=符号适配zsh @State private var editACLentryString: String = "chmod \\=a# 1 \"user:j.doe allow execute,writeattr,writeextattr,delete,delete_child,add_file,add_subdirectory,write,append,writesecurity,chown\" /Volumes/snfs1/pathTofile/5888709220764_p.jpg"
修改后将task.launchPath设置为/bin/zsh即可正常执行编辑ACL的命令。
内容的提问来源于stack exchange,提问作者Denis Andreev
相关产品推荐
相关产品推荐

