Linux bash子字符串在解释器模式下工作,但不在shell脚本中工作
当我尝试在解释器模式下在bashshell中执行子字符串时,我得到了预期的输出Linux bash子字符串在解释器模式下工作,但不在shell脚本中工作,linux,bash,shell,aix,Linux,Bash,Shell,Aix,当我尝试在解释器模式下在bashshell中执行子字符串时,我得到了预期的输出 bash-4.2$ x="SomeString" bash-4.2$ echo $x SomeString bash-4.2$ y=${x:0:4} bash-4.2$ echo $y Some bash-4.2$ 然而,在shell脚本中运行相同的命令时,我得到了一个错误 bash-4.2$ cat shell.sh x="SomeString" echo $x y=${x:0:4} echo $y bash-
bash-4.2$ x="SomeString"
bash-4.2$ echo $x
SomeString
bash-4.2$ y=${x:0:4}
bash-4.2$ echo $y
Some
bash-4.2$
然而,在shell脚本中运行相同的命令时,我得到了一个错误
bash-4.2$ cat shell.sh
x="SomeString"
echo $x
y=${x:0:4}
echo $y
bash-4.2$ sh shell.sh
SomeString
shell.sh[3]: y=${x:0:4}: 0403-011 The specified substitution is not valid for this command.
bash-4.2$
讽刺的是,当我通过bash-4.2$./shell.sh
调用shell时,它正在工作
这里发生了什么事
我在一台AIX机器上。子字符串是
bash
扩展。当您将其作为sh
运行时,它将禁用此扩展。使用bashshell.sh
你也应该把#/bin/bash
在脚本开头,以确保当您将其作为命令调用时,它与bash
一起运行。子字符串是bash
扩展。当您将其作为sh
运行时,它将禁用此扩展。使用bashshell.sh
你也应该把#/bin/bash
在脚本开头,以确保当您将其作为命令调用时,它与bash
一起运行。子字符串是bash
扩展。当您将其作为sh
运行时,它将禁用此扩展。使用bashshell.sh
你也应该把#/bin/bash
在脚本开头,以确保当您将其作为命令调用时,它与bash
一起运行。子字符串是bash
扩展。当您将其作为sh
运行时,它将禁用此扩展。使用bashshell.sh
你也应该把#/bin/bash
在脚本开头,以确保当您将其作为命令调用时,它与bash
一起运行。/bin/sh
!=<代码>/bin/bash
bash在作为调用时的行为不同。在某些系统上,/bin/sh
与/bin/bash
完全不同;它可能是dash
或原始的Bourne shell,或者。。。在AIX上,/bin/sh
可能是一个Korn shell。至少,在我现在使用的AIX机器上,/bin/sh
和/bin/ksh
具有相同的inode编号。用适当的shebang:#开始您的脚本/bin/bash
或任何指向bash的正确路径,/bin/sh
!=<代码>/bin/bash
bash在作为调用时的行为不同。在某些系统上,/bin/sh
与/bin/bash
完全不同;它可能是dash
或原始的Bourne shell,或者。。。在AIX上,/bin/sh
可能是一个Korn shell。至少,在我现在使用的AIX机器上,/bin/sh
和/bin/ksh
具有相同的inode编号。用适当的shebang:#开始您的脚本/bin/bash
或任何指向bash的正确路径,/bin/sh
!=<代码>/bin/bash
bash在作为调用时的行为不同。在某些系统上,/bin/sh
与/bin/bash
完全不同;它可能是dash
或原始的Bourne shell,或者。。。在AIX上,/bin/sh
可能是一个Korn shell。至少,在我现在使用的AIX机器上,/bin/sh
和/bin/ksh
具有相同的inode编号。用适当的shebang:#开始您的脚本/bin/bash
或任何指向bash的正确路径,/bin/sh
!=<代码>/bin/bash
bash在作为调用时的行为不同。在某些系统上,/bin/sh
与/bin/bash
完全不同;它可能是dash
或原始的Bourne shell,或者。。。在AIX上,/bin/sh
可能是一个Korn shell。至少,在我现在使用的AIX机器上,/bin/sh
和/bin/ksh
具有相同的inode编号。用适当的shebang:#开始您的脚本/bin/bash
或任何正确的bash路径,