Bash:语法错误:重定向意外 [英] Bash: Syntax error: redirection unexpected

查看:34
本文介绍了Bash:语法错误:重定向意外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在脚本中执行此操作:

I do this in a script:

read direc <<< $(basename `pwd`)

我得到:

Syntax error: redirection unexpected

在 ubuntu 机器上

in an ubuntu machine

/bin/bash --version
GNU bash, version 4.0.33(1)-release (x86_64-pc-linux-gnu)

虽然我在另一台 suse 机器上没有遇到这个错误:

while I do not get this error in another suse machine:

/bin/bash --version
GNU bash, version 3.2.39(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.

为什么会出错?

推荐答案

您的脚本是否在其散列行中引用了 /bin/bash/bin/sh ?Ubuntu 中的默认系统 shell 是 dash,而不是 bash,所以如果你有 #!/bin/sh 那么你的脚本将使用不同的 shell比你预期的要好.Dash 没有 <<< 重定向操作符.

Does your script reference /bin/bash or /bin/sh in its hash bang line? The default system shell in Ubuntu is dash, not bash, so if you have #!/bin/sh then your script will be using a different shell than you expect. Dash does not have the <<< redirection operator.

确保shebang行是:

Make sure the shebang line is:

#!/bin/bash

#!/usr/bin/env bash

并使用以下命令运行脚本:

And run the script with:

$ ./script.sh

不要不要使用显式的sh运行它,因为这会忽略shebang:

Do not run it with an explicit sh as that will ignore the shebang:

$ sh ./script.sh   # Don't do this!

这篇关于Bash:语法错误:重定向意外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆