【发布时间】:2016-08-01 15:18:08
【问题描述】:
我无法解决如何使用双引号来捕获 bash 脚本中的命令行参数。我有两个文件:hello_world 和 hello world(注意第二个文件名中的空格)。
当然可以:
#!/usr/bin/env bash
ls "$@"
$ ./quoted_args.sh hello_world "hello world"
hello world hello_world
但是,以下(非常相似的)脚本都不起作用:
脚本 A:
#!/usr/bin/env bash
FILES="$@"
ls "$FILES"
$ ./quoted_args.sh hello_world "hello world"
ls: hello_world hello world: No such file or director
脚本 B:
#!/usr/bin/env bash
FILES=$@
ls "$FILES"
$ ./quoted_args.sh hello_world "hello world"
ls: hello_world hello world: No such file or director
脚本 C:
#!/usr/bin/env bash
FILES="$@"
ls $FILES
$ ./quoted_args.sh hello_world "hello world"
ls: hello: No such file or directory
ls: world: No such file or directory
hello_world
脚本 D:
#!/usr/bin/env bash
FILES=$@
ls $FILES
$ ./quoted_args.sh hello_world "hello world"
ls: hello: No such file or directory
ls: world: No such file or directory
hello_world
我觉得我已经尝试了各种方法。我将不胜感激任何帮助或见解!
【问题讨论】:
标签: bash command-line-arguments quotes