I do this in a script:
read direc <<< $(basename `pwd`)
and I get:
Syntax error: redirection unexpected
in an ubuntu machine
GNU bash, version 4.0.33(1)-release (x86_64-pc-linux-gnu)
while I do not get this error in another suse machine:
GNU bash, version 3.2.39(1)-release (x86_64-suse-linux-gnu)
Copyright (C) 2007 Free Software Foundation, Inc.
Why the error?
Does your script reference
/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.
Make sure the shebang line is:
And run the script with:
Do not run it with an explicit
sh as that will ignore the shebang:
sh ./script.sh # Don't do this!
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.
I was getting this problem from my Dockerfile as I had:
RUN bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)
However, according to this issue, it was solved:
The exec form makes it possible to avoid shell string munging, and
RUN commands using a base image that does not contain
To use a different shell, other than
/bin/sh, use the exec form
passing in the desired shell. For example,
RUN ["/bin/bash", "-c", "echo hello"]
RUN ["/bin/bash", "-c", "bash < <(curl -s -S -L https://raw.githubusercontent.com/moovweb/gvm/master/binscripts/gvm-installer)"]
Notice the quotes around each parameter.
You can get the output of that command and put it in a variable. then use
heredoc. for example:
nc -l -p 80 <<< "tested like a charm";
can be written like:
nc -l -p 80 <<EOF
tested like a charm
and like this (this is what you want):
text="tested like a charm"
nc -l -p 80 <<EOF
Practical example in
[email protected]:~$ docker run --rm -it busybox
/ # nc -l -p 80 <<< "tested like a charm";
sh: syntax error: unexpected redirection
/ # nc -l -p 80 <<EOL
> tested like a charm
^Cpunt! => socket listening, no errors. ^Cpunt! is result of CTRL+C signal.
/ # text="tested like a charm"
/ # nc -l -p 80 <<EOF
do it the simpler way,
Or use the shell
Another reason to the error may be if you are running a cron job that updates a subversion working copy and then has attempted to run a versioned script that was in a conflicted state after the update…
On my machine, if I run a script directly, the default is
If I run it with
sudo, the default is
That’s why I was hitting this problem when I used
In my case error is because i have put “>>” twice
mongodump --db=$DB_NAME --collection=$col --out=$BACKUP_LOCATION/$DB_NAME-$BACKUP_DATE >> >> $LOG_PATH
i just correct it as
mongodump --db=$DB_NAME --collection=$col --out=$BACKUP_LOCATION/$DB_NAME-$BACKUP_DATE >> $LOG_PATH
Before running the script, you should check first line of the shell script for the interpreter.
if scripts starts with /bin/bash , run the script using the below command
if script starts with /bin/sh, run the script using the below command
./sample.sh – This will detect the interpreter from the first line of the script and run.
Different Linux distributions having different shells as default.