简体   繁体   English

bash:替换错误

[英]bash : Bad Substitution

#!/bin/bash

jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}

This bash script gives me Bad substitution error on ubuntu. Any help will be highly appreciated.这个 bash 脚本给了我 ubuntu 上的错误替换错误。任何帮助将不胜感激。

The default shell ( /bin/sh ) under Ubuntu points to dash , not bash . Ubuntu 下的默认 shell ( /bin/sh ) 指向dash ,而不是bash

me@pc:~$ readlink -f $(which sh)
/bin/dash

So if you chmod +x your_script_file.sh and then run it with ./your_script_file.sh , or if you run it with bash your_script_file.sh , it should work fine.因此,如果您chmod +x your_script_file.sh然后使用./your_script_file.sh运行它,或者如果您使用bash your_script_file.sh运行它,它应该可以正常工作。

Running it with sh your_script_file.sh will not work because the hashbang line will be ignored and the script will be interpreted by dash , which does not support that string substitution syntax.使用sh your_script_file.sh运行它将不起作用,因为 hashbang 行将被忽略并且脚本将由dash解释,它不支持该字符串替换语法。

I had the same problem.我有同样的问题。 Make sure your script didnt have确保你的脚本没有

#!/bin/sh 

at the top of your script.在脚本的顶部。 Instead, you should add相反,您应该添加

#!/bin/bash

对于到达这里的其他人,使用 env 变量语法进行命令时也会出现此确切消息,例如${which sh}而不是正确的$(which sh)

Your script syntax is valid bash and good.您的脚本语法是有效的 bash 并且很好。

Possible causes for the failure:失败的可能原因:

  1. Your bash is not really bash but ksh or some other shell which doesn't understand bash's parameter substitution.您的bash并不是真正的 bash,而是ksh或其他一些无法理解 bash 参数替换的 shell。 Because your script looks fine and works with bash.因为您的脚本看起来不错并且可以与 bash 配合使用。 Do ls -l /bin/bash and check it's really bash and not sym-linked to some other shell.执行ls -l /bin/bash并检查它是否真的是 bash 而不是符号链接到其他一些 shell。

  2. If you do have bash on your system, then you may be executing your script the wrong way like: ksh script.sh or sh script.sh (and your default shell is not bash).如果您的系统上确实有 bash,那么您可能会以错误的方式执行脚本,例如: ksh script.shsh script.sh (并且您的默认 shell 不是 bash)。 Since you have proper shebang, if you have bash ./script.sh or bash ./script.sh should be fine.由于您有适当的shebang,如果您有 bash ./script.shbash ./script.sh应该没问题。

尝试使用 bash 命令显式运行脚本,而不仅仅是将其作为可执行文件执行。

Also, make sure you don't have an empty string for the first line of your script.另外,请确保脚本的第一行没有空字符串。

ie make sure #!/bin/bash is the very first line of your script.即确保#!/bin/bash是脚本的第一行。

Not relevant to your example, but you can also get the Bad substitution error in Bash for any substitution syntax that Bash does not recognize.与您的示例无关,但是对于 Bash 无法识别的任何替换语法,您也可以在 Bash 中获得Bad substitution错误。 This could be:这可能是:

  • Stray whitespace.杂散的空白。 Eg bash -c '${x }'例如bash -c '${x }'
  • A typo.一个错字。 Eg bash -c '${x;-}'例如bash -c '${x;-}'
  • A feature that was added in a later Bash version.在更高版本的 Bash 中添加的功能。 Eg bash -c '${x@Q}' before Bash 4.4.例如bash -c '${x@Q}'在 Bash 4.4 之前。

If you have multiple substitutions in the same expression, Bash may not be very helpful in pinpointing the problematic expression.如果您在同一个表达式中有多个替换,Bash 可能无法帮助确定有问题的表达式。 Eg:例如:

$ bash -c '"${x } multiline string
$y"'
bash: line 1: ${x } multiline string
$y: bad substitution

Both - bash or dash - work, but the syntax needs to be:两者 - bash 或 dash - 都可以工作,但语法需要是:

FILENAME=/my/complex/path/name.ext
NEWNAME=${FILENAME%ext}new

I was adding a dollar sign twice in an expression with curly braces in bash:我在 bash 中带花括号的表达式中添加了两次美元符号:

cp -r $PROJECT_NAME ${$PROJECT_NAME}2

instead of代替

cp -r $PROJECT_NAME ${PROJECT_NAME}2

我发现这个问题要么是由标记的答案引起的,要么是在 bash 声明之前有一行或一个空格

Looks like "+x" causes problems:看起来像“+x”会导致问题:

root@raspi1:~# cat > /tmp/btest
#!/bin/bash

jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}
root@raspi1:~# chmod +x /tmp/btest
root@raspi1:~# /tmp/btest
root@raspi1:~# sh -x /tmp/btest
+ jobname=job_201312161447_0003
/tmp/btest: 4: /tmp/btest: Bad substitution
#!/bin/bash

jobname="job_201312161447_0003"
jobname_pre=${jobname:0:16}
jobname_post=${jobname:17}

This bash script gives me Bad substitution error on ubuntu.这个 bash 脚本在 ubuntu 上给了我坏替换错误。 Any help will be highly appreciated.任何帮助将不胜感激。

in my case (under ubuntu 18.04), I have mixed $( ${} ) that works fine:在我的情况下(在 ubuntu 18.04 下),我混合了$( ${} )工作正常:

BACKUPED_NB=$(ls ${HOST_BACKUP_DIR}*${CONTAINER_NAME}.backup.sql.gz | wc --lines)

full example here .完整的例子在这里

I used #!bin/bash as well tried all approaches like no line before or after #.bin/bash.我使用#!bin/bash并尝试了所有方法,比如在#.bin/bash.
Then also tried using +x but still didn't work.然后也尝试使用 +x 但仍然没有用。 Finally i tried running the script ./script.sh it worked fine.最后我尝试运行脚本./script.sh它工作正常。

#!/bin/bash
jobname="job_201312161447_0003"
jobname_post=${jobname:17}

root@ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts# sh jaru.sh root@ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts#sh jaru.sh
jaru.sh: 3: jaru.sh: Bad substitution jaru.sh: 3: jaru.sh: 替换错误

root@ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts#./jaru.sh root@ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts#./jaru.sh
root@ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts# root@ip-10-2-250-36:/home/bitnami/python-module/workflow_scripts#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM