Bash script prints "Command Not Found" on empty lines

LinuxBashDebian

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

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

Solution 3 - Linux

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

Solution 4 - Linux

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)

Solution 5 - Linux

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

Solution 6 - Linux

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.

Solution 7 - Linux

use dos2unix on your script file.

Solution 8 - Linux

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

/home/Manuel/mywrittenscript

Solution 9 - Linux

Solution 10 - Linux

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.

Solution 11 - Linux

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

Solution 12 - Linux

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.

Solution 13 - Linux

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.

Solution 14 - Linux

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

Solution 15 - Linux

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

Solution 16 - Linux

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavidView Question on Stackoverflow
Solution 1 - LinuxchownView Answer on Stackoverflow
Solution 2 - LinuxclyfishView Answer on Stackoverflow
Solution 3 - LinuxLypso345View Answer on Stackoverflow
Solution 4 - LinuxaltamontView Answer on Stackoverflow
Solution 5 - LinuxMichael FreidgeimView Answer on Stackoverflow
Solution 6 - LinuxpaxdiabloView Answer on Stackoverflow
Solution 7 - Linuxbash-o-logistView Answer on Stackoverflow
Solution 8 - LinuxMasood MoghiniView Answer on Stackoverflow
Solution 9 - LinuxAminah NurainiView Answer on Stackoverflow
Solution 10 - LinuxJuniarView Answer on Stackoverflow
Solution 11 - LinuxgjinView Answer on Stackoverflow
Solution 12 - LinuxMarek WysockiView Answer on Stackoverflow
Solution 13 - LinuxStefan van den AkkerView Answer on Stackoverflow
Solution 14 - LinuxCNSKnightView Answer on Stackoverflow
Solution 15 - LinuxMike GoethalsView Answer on Stackoverflow
Solution 16 - LinuxKean AmaralView Answer on Stackoverflow