Bash: Syntax error: redirection unexpected

后端 未结 9 1568
终归单人心
终归单人心 2020-11-27 12:30

I do this in a script:

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

and I get:

Syntax error: redirection unexpected

相关标签:
9条回答
  • 2020-11-27 13:00

    Before running the script, you should check first line of the shell script for the interpreter.

    Eg: if scripts starts with /bin/bash , run the script using the below command "bash script_name.sh"

    if script starts with /bin/sh, run the script using the below command "sh script_name.sh"

    ./sample.sh - This will detect the interpreter from the first line of the script and run.

    Different Linux distributions having different shells as default.

    0 讨论(0)
  • 2020-11-27 13:01

    do it the simpler way,

    direc=$(basename `pwd`)
    

    Or use the shell

    $ direc=${PWD##*/}
    
    0 讨论(0)
  • 2020-11-27 13:02

    If you're using the following to run your script:

    sudo sh ./script.sh
    

    Then you'll want to use the following instead:

    sudo bash ./script.sh
    

    The reason for this is that Bash is not the default shell for Ubuntu. So, if you use "sh" then it will just use the default shell; which is actually Dash. This will happen regardless if you have #!/bin/bash at the top of your script. As a result, you will need to explicitly specify to use bash as shown above, and your script should run at expected.

    Dash doesn't support redirects the same as Bash.

    0 讨论(0)
提交回复
热议问题