133

Every time I run a script using bash scriptname.sh from the command line in Debian, I get Command Not found and then the result of the script.

The script works but there is always a Command Not Found statement printed on screen for each empty line. Each blank line is resulting in a command not found.

I am running the script from the /var folder.

Here is the script:

#!/bin/bash

echo Hello World

I run it by typing the following:

bash testscript.sh

Why would this occur?

jww
  • 97,681
  • 90
  • 411
  • 885
David
  • 1,765
  • 3
  • 14
  • 9
  • 2
    Seriously, net is full of such complaints. Shell is very weak and lame interpreter. Little here and there and it fails. You got to watch even all white spaces in script. My script was failing because of invisible line feed character. Probably this is only scripting language where white spaces count matters! – Atul Feb 13 '20 at 16:48
  • 5
    Use `bash -x scriptname.sh` to trace the error. – In my case it was a sh-file saved under Windows with VSCode and line endings as "CRLF". In VSCode in the bottom right corner you can change the line terminator from "CRLF" to "LF". Uploaded this file and could execute it finally with `bash scriptname.sh`. – Avatar Mar 24 '20 at 15:47
  • This is definitely a duplicate of https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings but many answers here explain a number of other situations where you get "command not found" for other reasons. Visitors are advised to **read all the answers** if your problem is not specifically about empty lines. – tripleee Aug 01 '20 at 10:43
  • 1
    @Atul I can't agree with you more. That's why I write scripts in JavaScript, which makes it much easier to re-use modular code. Python works for this too. – trusktr Jun 07 '21 at 20:48

17 Answers17

197

Make sure your first line is:

#!/bin/bash

Enter your path to bash if it is not /bin/bash


Try running:

dos2unix script.sh

That wil convert line endings, etc from Windows to unix format. i.e. it strips \r (CR) from line endings to change them from \r\n (CR+LF) to \n (LF).

More details about the dos2unix command (man page)


Another way to tell if your file is in dos/Win format:

cat scriptname.sh | sed 's/\r/<CR>/'

The output will look something like this:

#!/bin/sh<CR>
<CR>
echo Hello World<CR>
<CR>

This will output the entire file text with <CR> displayed for each \r character in the file.

janos
  • 120,954
  • 29
  • 226
  • 236
chown
  • 51,908
  • 16
  • 134
  • 170
  • That's probably not necessary since he's running it directly with `bash scriptname.sh` (but it's still good practice, of course). – paxdiablo Sep 09 '11 at 13:51
  • 1
    Hi #!/bin/bash is the first line of my script – David Sep 09 '11 at 13:53
  • That is true @paxdiablo. david - can you post your entire script contents please? – chown Sep 09 '11 at 13:55
  • Script updated. I dont think its the script at fault - its probably the most basic a script can get – David Sep 09 '11 at 13:56
  • Can u also put the output of running 'env' commend? – chown Sep 09 '11 at 13:59
  • Quotes dont make a different but removing the blank line gets rid of the problem. It appears that for each blank line i have i get a command not found printed to the screen? – David Sep 09 '11 at 14:03
  • @chown - spot on - cheers. It was edited in windows and its done something to the file. If i create the same file through vi on the command line then i dont have the issue. Is this something to beware of when working between windows and linux? – David Sep 09 '11 at 14:12
  • Yes, windows uses \r\n (CR+LF) where unix only uses \n (LF) for line endings. Some things are ok with windows format on unix, but some times it does break files. – chown Sep 09 '11 at 14:15
  • I thought it might be the CR+LF problem. – Almo Sep 09 '11 at 14:24
  • No, but consider that bash on linux uses `\n` as the end-of-line sequence, and if the line contains `\r\n` (due to windows text editor), then bash clearly wants to do something with the `\r` "word". – glenn jackman Sep 09 '11 at 15:13
  • 1
    @chown - I had the same issue on Mac. I stumbled across this post. I am not sure if it helped the OP or not. But your solution helped me. – Prashant Oct 22 '11 at 19:41
  • 1
    thanks. This was the exact the issue I was facing. This answers got it working. – slayedbylucifer Sep 28 '12 at 04:39
  • 5
    How can you write an answer to a programming question while drive? – Omar Tariq Sep 05 '16 at 16:24
  • Sorry officer, I didn't mean to run the red light, I was saving the world (of windows users) on stack overflow – Davos Jul 13 '17 at 03:43
  • If you are running out of options in solving the above problem, give this answer a try. Totally saved me. – impulse Mar 15 '19 at 08:22
76

You can use bash -x scriptname.sh to trace it.

clyfish
  • 10,240
  • 2
  • 31
  • 23
63

I also ran into a similar issue. The issue seems to be permissions. If you do an ls -l, you may be able to identify that your file may NOT have the execute bit turned on. This will NOT allow the script to execute. :)

As @artooro added in comment:

To fix that issue run chmod +x testscript.sh

CommonSenseCode
  • 23,522
  • 33
  • 131
  • 186
Lypso345
  • 729
  • 5
  • 2
25

This might be trivial and not related to the OP's question, but I often made this mistaken at the beginning when I was learning scripting

VAR_NAME = $(hostname)
echo "the hostname is ${VAR_NAME}"  

This will produce 'command not found' response. The correct way is to eliminate the spaces

VAR_NAME=$(hostname)
altamont
  • 353
  • 4
  • 8
18

On Bash for Windows I've tried incorrectly to run

run_me.sh 

without ./ at the beginning and got the same error.

For people with Windows background the correct form looks redundant:

./run_me.sh
Michael Freidgeim
  • 26,542
  • 16
  • 152
  • 170
  • Same on a linux-gnu bash system: `myscript` returns `bash: myscript: command not found`; `./myscript` works – PaulH May 31 '21 at 10:58
13

If the script does its job (relatively) well, then it's running okay. Your problem is probably a single line in the file referencing a program that's either not on the path, not installed, misspelled, or something similar.

One way is to place a set -x at the top of your script or run it with bash -x instead of just bash - this will output the lines before executing them and you usually just need to look at the command output immediately before the error to see what's causing the problem

If, as you say, it's the blank lines causing the problems, you might want to check what's actaully in them. Run:

od -xcb testscript.sh

and make sure there's no "invisible" funny characters like the CTRL-M (carriage return) you may get by using a Windows-type editor.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
9

use dos2unix on your script file.

bash-o-logist
  • 6,665
  • 1
  • 17
  • 14
7

for executing that you must provide full path of that for example

/home/Manuel/mywrittenscript
Masood Moghini
  • 374
  • 3
  • 13
  • Indeed, the shell does not care about file extensions at all. Not sure this merits being posted as a separate answer. – tripleee Jul 31 '16 at 20:13
  • of course .in linux file system contrary to windows extensions are used merely as a part of the name and say nothing about file's content.I mustn't say **in my case** constraint. – Masood Moghini Aug 01 '16 at 10:05
  • thanks this helped me, I was giving just the filename and now it runs after I give it correct path – obadul024 Jan 21 '22 at 14:00
6

Try chmod u+x testscript.sh

I know it from here: http://www.linuxquestions.org/questions/red-hat-31/running-shell-script-command-not-found-202062/

Aminah Nuraini
  • 18,120
  • 8
  • 90
  • 108
6

If you have Notepad++ and you get this .sh Error Message: "command not found" or this autoconf Error Message "line 615: ../../autoconf/bin/autom4te: No such file or directory".

On your Notepad++, Go to Edit -> EOL Conversion then check Macinthos(CR). This will edit your files. I also encourage to check all files with this command, because soon such an error will occur.

Juniar
  • 1,269
  • 1
  • 15
  • 24
  • Thanks for this. For my case I was running script on busybox linux distro. Had same error "not found" in all the empty lines in my scripts. it also had problems with if/else statement. changing EOL in Notepad++ to Unix fixed it. – GeneCode Sep 07 '17 at 02:04
4

Had the same problem. Unfortunately

dos2unix winfile.sh
bash: dos2unix: command not found

so I did this to convert.

 awk '{ sub("\r$", ""); print }' winfile.sh > unixfile.sh

and then

bash unixfile.sh
gjin
  • 860
  • 1
  • 14
  • 28
2

Problems with running scripts may also be connected to bad formatting of multi-line commands, for example if you have a whitespace character after line-breaking "\". E.g. this:

./run_me.sh \ 
--with-some parameter

(please note the extra space after "\") will cause problems, but when you remove that space, it will run perfectly fine.

1

Make sure you haven´t override the 'PATH' variable by mistake like this:

#!/bin/bash
PATH="/home/user/Pictures/"; # do NOT do this

This was my mistake.

mmathieum
  • 506
  • 5
  • 14
0

I was also having some of the Cannot execute command. Everything looked correct, but in fact I was having a non-breakable space &nbsp; right before my command which was ofcourse impossible to spot with the naked eye:

if [[ "true" ]]; then
  &nbsp;highlight --syntax js "var i = 0;"
fi

Which, in Vim, looked like:

if [[ "true" ]]; then
  highlight --syntax js "var i = 0;"
fi

Only after running the Bash script checker shellcheck did I find the problem.

Stefan van den Akker
  • 6,661
  • 7
  • 48
  • 63
  • `shellcheck` is available on-line, though you of course need to copy+paste your script exactly in order for that to help. http://shellcheck.net/ – tripleee Mar 02 '17 at 08:18
0

I ran into this today, absentmindedly copying the dollar command prompt $ (ahead of a command string) into the script.

CNSKnight
  • 567
  • 7
  • 14
-2

Add the current directory ( . ) to PATH to be able to execute a script, just by typing in its name, that resides in the current directory:

PATH=.:$PATH
-2

You may want to update you .bashrc and .bash_profile files with aliases to recognize the command you are entering.

.bashrc and .bash_profile files are hidden files probably located on your C: drive where you save your program files.

Kean Amaral
  • 5,503
  • 2
  • 16
  • 9