Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I created this script to backup my postgresql database with cron:

backup.sh

#!/bin/bash
export PGUSER="user"
export PGPASSWORD="pass"
FECHA_ACTUAL=`date +%Y-%m-%d`
HORA_ACTUAL=`date +%H:%M`
ARCH_RESP=$FECHA_ACTUAL-$HORA_ACTUAL
pg_dump -O -Fc mydb -h localhost > /home/user/backups/backup_$ARCH_RESP.sql
find /home/user/backups/ -name '*.sql' -mtime +2 -exec rm -f {} \;
unset PGUSER
unset PGPASSWORD

If I copy and paste this code on the terminal it works fine, but if I try to run the script, I get this error:

user@dental:~/scripts$ ./backup.sh
export: bad interpreter: no such file or directory

Is there something wrong with my script? Or is it the wrong interpreter as it says?

share|improve this question
1  
Whats the output of type -a bash? – heemayl yesterday
    
Please edit the question and post the output of this command: head -n 1 backup.sh | od -c. – terdon yesterday
1  
Your file is probably not in Linux format. if the output of [ file <your-file> ] tells "ASCII text, with CRLF line terminators", then change format using dos2unix. – tonioc yesterday
up vote 11 down vote accepted

I can get the same error if the first line is terminated only with a CR (instead of LF):

$ echo -en '#!/bin/bash\rexport foo=bar\n' > test.sh
$ chmod +x test.sh
$ ./test.sh
export: bad interpreter: No such file or directory

What happens is that the kernel looks for an interpreter program called /bin/bash\rexport, doesn't find it, and drops an error. Bash prints an error message with the name of the file

bash: ./test.sh: /bin/bash\rexport: bad interpreter: No such file or directory

but since the carriage return moves the output back to the front of the line, you see only

export: bad interpreter: No such file or directory

So the problem seems to be with the line endings.

Note that with a DOS-style CRLF line ending, the result is different, since there is an LF to end the line now.

$ echo -en '#!/bin/bash\r\nexport foo=bar\n' > test.sh
$ ./test.sh    
bash: ./test.sh: /bin/bash^M: bad interpreter: No such file or directory

Though I don't know why bash seems to quote the CR on output as ^M this time.


IIRC the lone CR as a line ending is a remnant of old Mac systems, and dos2unix doesn't seem to fix it by default. You'd need to use mac2unix or dos2unix -c mac.

Something like this should also turn all of CR or CRLF to Unix-style LF line endings, if you happen to have all styles mixed up and have no need for the CR's in any other sense.

sed 's/\r/\n/g;s/\n$//' backup.sh
share|improve this answer
    
Thanks it worked with the sed 's/\r/\n/g;s/\n$//' backup.sh – Elros Romeo yesterday
    
What did I do wrong with when I created my sh? – Elros Romeo yesterday
    
@ElrosRomeo That's harder to say, since we don't how you created it. Some editors can choose the line ending style, so it might just be a misconfiguration on the editor. – ilkkachu yesterday
    
Thanks, I wrote it with mousepad and saved it with sh extension, then I chmod the file with 777 – Elros Romeo yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.