过去的旧建议是对任何涉及 a 的表达式加双引号$VARIABLE
,至少在希望 shell 将其解释为单个项目的情况下,否则,内容中的任何空格$VARIABLE
都会脱离 shell。
但是,我知道在较新版本的 shell 中,不再总是需要双引号(至少出于上述目的)。例如,在bash
:
% FOO='bar baz'
% [ $FOO = 'bar baz' ] && echo OK
bash: [: too many arguments
% [[ $FOO = 'bar baz' ]] && echo OK
OK
% touch 'bar baz'
% ls $FOO
ls: cannot access bar: No such file or directory
ls: cannot access baz: No such file or directory
Run Code Online (Sandbox Code Playgroud)
在zsh
,而另一方面,同样的三个命令成功。因此,基于此实验,似乎在 中bash
可以省略 内部的双引号[[ ... ]]
,但不能省略内部 …
在 bash 子 shell 中,运行 cd 时出现以下错误
sudo: cd: command not found
Run Code Online (Sandbox Code Playgroud)
这是意料之中的,因为我没有路径。通常要解决这个问题,我只提供完整路径,如下所示:(/usr/local/bin/foo)
令我惊讶的是,cd
它似乎不在任何正常的地方。
which cd
whereis cd
ls /bin | grep cd
Run Code Online (Sandbox Code Playgroud)
相比之下,ls
正是我所期望的。
which ls
/bin/ls
Run Code Online (Sandbox Code Playgroud)
cd
命令位于何处?为什么与所有其他命令不同?
更新
另一个有趣的花絮,cd 没有出现在 hash
hash
0 /bin/ls
2 /usr/bin/find
2 /sbin/ip
1 /usr/bin/updatedb
1 /usr/bin/apt-get
Run Code Online (Sandbox Code Playgroud) 我试图在将条件附加到 profile.d 文件之前进行比较
Agrep -F 'TMOUT' /etc/profile.d/sh.local
准确地显示了我的期望,但是测试总是显示正确。在所有这些示例中,我已经将我想要的信息附加到 sh.local 但如果它存在我不想再做一次。
[ ! `grep -Fq 'TMOUT' /etc/profile.d/sh.local` ] && echo $?
0
[ ! `grep -Fq 'NOT_PRESENT' /etc/profile.d/sh.local` ] && echo $?
0
Run Code Online (Sandbox Code Playgroud)
尽管这是针对 .ebextension 命令的测试在我什至尝试部署之前都失败了,但我想最终结果应该是这样的,但我显然错了。我试过使用 $(grep) 和 `grep`。还有 ==1 对 !
[ ! `grep -Fq 'TMOUT' /etc/profile.d/sh.local` ] && echo $?
0
[ ! `grep -Fq 'NOT_PRESENT' /etc/profile.d/sh.local` ] && echo $?
0
Run Code Online (Sandbox Code Playgroud)
作为记录,这是 Amazon Linux 2 和以下命令吐出以下内容
01_hard_5.4.5_shell_timeout:
test: "[ $(grep -Fxq 'TMOUT' /etc/profile.d/sh.local) ==1 ]"
command: | …
Run Code Online (Sandbox Code Playgroud) 打开终端时出现以下异常。我该如何解决这个错误:
bash: /etc/profile.d/proxy-globalmenu.sh: line 1: syntax error in conditional expression
bash: /etc/profile.d/proxy-globalmenu.sh: line 1: syntax error near `]'
bash: /etc/profile.d/proxy-globalmenu.sh: line 1: `if [[ "$DESKTOP_SESSION" = "cinnamon" ] || ["$DESKTOP_SESSION" = gnome"] && [ -z "$UBUNTU_MENUPROXY" ]]; then'
[snoop@lenovo ~]$
Run Code Online (Sandbox Code Playgroud)
请注意,我在安装全局菜单 gnome 扩展后发现此错误。
我写了那个脚本,但如果我运行它,我会得到这个输出:
./autotex: line 7: syntax error near unexpected token `then'
./autotex: line 7: ` then'
Run Code Online (Sandbox Code Playgroud)
脚本是:
#!/bin/bash
while [ $key = "q"]; do
dateTex = grep $1.tex| cut -b 43 - 54
datePdf = grep $1.pdf| cut -b 43 - 54
if[$dateTex !eq $datePdf]
then
pdflatex $1.tex
fi
read -t 1 -n 1 key
done
Run Code Online (Sandbox Code Playgroud) bash ×4
shell ×3
shell-script ×2
cd-command ×1
fedora ×1
fhs ×1
gnome3 ×1
quoting ×1
test ×1
zsh ×1