$ ls -l /tmp/test/my\ dir/
total 0
Run Code Online (Sandbox Code Playgroud)
我想知道为什么以下运行上述命令的方法失败或成功?
$ abc='ls -l "/tmp/test/my dir"'
$ $abc
ls: cannot access '"/tmp/test/my': No such file or directory
ls: cannot access 'dir"': No such file or directory
$ "$abc"
bash: ls -l "/tmp/test/my dir": No such file or directory
$ bash -c $abc
'my dir'
$ bash -c "$abc"
total 0
$ eval $abc
total 0
$ eval "$abc"
total 0
Run Code Online (Sandbox Code Playgroud) > echo "hi"
hi
> VAR='echo "hi"'
> $VAR
"hi"
Run Code Online (Sandbox Code Playgroud)
为什么上述命令的输出不同?
单引号也会发生类似的事情:
> VAR="echo 'hi'"
> $VAR
> 'hi'
Run Code Online (Sandbox Code Playgroud) 我想在 bash 脚本中运行这样的命令:
freebcp <authentication and other parameters> -t "," -r "\r\n"
Run Code Online (Sandbox Code Playgroud)
直接在命令行上运行时,该命令按预期工作。但是当放在 bash 脚本中的变量中时,它会返回如下错误:
Msg 20104, Level 3
Unexpected EOF encountered in bcp datafile
Msg 20074, Level 11
Attempt to bulk copy an oversized row to the server
bcp copy in failed
Run Code Online (Sandbox Code Playgroud)
当命令放在变量中并且双引号被转义时:
cmd="freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t \",\" -r \"\r\n\" -c"
`$cmd`
Run Code Online (Sandbox Code Playgroud)
注意:将以下内容按预期工作:
`freebcp ${db_name}.dbo.${table_name} in ${p_file} -S ${server_name} -U ${username} -P ${password} -t "," -r "\r\n" -c`
Run Code Online (Sandbox Code Playgroud)
所以我知道有一些引用/转义/扩展问题,但我不知道如何解决它。
注 2 …