Every command is returning 'bash: command : command not found...'
Problem is with export PATH=$SCALA_HOME/bin:PATH
in this you missed $
.
Update default variable as PATH=$SCALA_HOME/bin:$PATH
Bash script prints Command Not Found on empty lines
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.
bash: ????: command not found
First you run:
echo ll > test && chmod +x test
then these cases.
Case 3:
When you execute:
. test
it is equivalent of:
source test
source
is a shell builtin that tells the shell to read the given script file and execute the commands in the current shell environment. However since current path or .
is not in your path, it finds test
using PATH
environment variable which is /bin/test
.
/bin/test
is not really a script file that can be read/executed by source
; it ends up reading a binary file and errors out since that file is a binary file, not an ascii text file and errors out writing:
????: command not found
You will get same behavior when you run source date
or source ls
as these are all binary files.
Case 1:
You are executing shell builtin test
without any arguments that makes it exits with exit value: 1
Case 2:
When you run ./test
it attempts to run ll
and alias
is not available in spawned sub-shell hence it cannot find alias ll
. Due to that fact it exits with exit value: 127
with ./test: line 1: ll: command not found
error on stderr
.
Case 4:
. ./test
is same as source ./test
that run in current shell only. Therefore it is able to find alias you've set earlier for ll
hence it runs aliased command ls -alFh
and exits with 0
How can I fix a command not found error given for a variable assignment in my Bash script?
A sample execution of your script returns following (error) output
sh ./test.sh
Enter assignment mark (0 to 40):20
Enter Test1 mark (0 to 15):3
Enter Test2 mark (1 to 15):10
Enter Final exam mark (1 to 30):20
./test.sh: line 8: grades: command not found
./test.sh: line 9: [: -ge: unary operator expected
./test.sh: line 12: [: -ge: unary operator expected
./test.sh: line 15: [: -ge: unary operator expected
./test.sh: line 18: [: -ge: unary operator expected
./test.sh: line 21: [: -ge: unary operator expected
./test.sh: line 24: [: -ge: unary operator expected
./test.sh: line 27: [: -ge: unary operator expected
./test.sh: line 30: [: -ge: unary operator expected
./test.sh: line 33: [: -ge: unary operator expected
./test.sh: line 36: [: -ge: unary operator expected
./test.sh: line 39: [: -ge: unary operator expected
./test.sh: line 42: [: -ge: unary operator expected
./test.sh: line 45: [: -ge: unary operator expected
Wrong input
Where the error ./test.sh: line 8: grades: command not found
implies that there must be something wrong with your grades
assignment (and possibly the calculation).
grades = $(($assignment + $testo + $testa + $finalexam))
If you want to use this ((result=a+b))
bash syntax for your integer arithmetic and variable assignment then you need to change it to
((grades = assignment + testo + testa + finalexam))
Then the sample output will be as expected
sh ./test.sh
Enter assignment mark (0 to 40):20
Enter Test1 mark (0 to 15):3
Enter Test2 mark (1 to 15):10
Enter Final exam mark (1 to 30):20
Your final grade is D
Addendum
As pointed out @BenjaminW. in the comment Another thing to fix (and maybe point out) is the quoting of variables in [...]
; in your if-statement(s)
if [ $grades -ge 0 ] && [ $grades -lt 49 ]
you reference to your $grades
variable without enclosing it in double quotes. That's usually not a good idea (and a source of errors as well as confusion) because referring to a variable without double quotes allows for reinterpretation (of therein contained special characters) and word splitting (if there are space characters) rather than simply only replacing the variable with its value – which is what you actually want. Thus, your if-statement(s) should use "$grades"
instead as in
if [ "$grades" -ge 0 ] && [ "$grades" -lt 49 ]
But there's more. Because you're using Bash prefer using [[ ]]
instead of [ ]
; and then you also don't need to expand your variable with $
for your arithmetic comparisons. With this your if-statement becomes
if [[ grades -ge 0 && grades -lt 49 ]]
Now given that you want to do arithmetic comparisons you should actually use (( ))
, and then your if-statement looks like this
if (( grades >= 0 && grades < 49 ))
The revised Bash script is then this
#!/bin/bash
read -p "Enter assignment mark (0 to 40):" assignment
read -p "Enter Test1 mark (0 to 15):" testo
read -p "Enter Test2 mark (1 to 15):" testa
read -p "Enter Final exam mark (1 to 30):" finalexam
((grades = assignment + testo + testa + finalexam))
if (( grades >= 0 && grades < 49 ))
then
echo "Your final grade is F"
elif (( grades >= 50 && grades < 52 ))
then
echo "Your final grade is D-"
elif (( grades >= 53 && grades < 56 ))
then
echo "Your final grade is D"
elif (( grades >= 57 && grades < 59 ))
then
echo "Your final grade is D+"
elif (( grades >= 60 && grades < 62 ))
then
echo "Your final grade is C-"
elif (( grades >= 63 && grades < 66 ))
then
echo "Your final grade is C"
elif (( grades >= 67 && grades < 69 ))
then
echo "Your final grade is C+"
elif (( grades >= 70 && grades < 72 ))
then
echo "Your final grade is B-"
elif (( grades >= 73 && grades < 76 ))
then
echo "Your final grade is B"
elif (( grades >= 77 && grades < 79 ))
then
echo "Your final grade is B+"
elif (( grades >= 80 && grades < 84 ))
then
echo "Your final grade is A-"
elif (( grades >= 85 && grades < 89 ))
then
echo "Your final grade is A"
elif (( grades >= 90 && grades < 100 ))
then
echo "Your final grade is A+"
else
echo "Wrong input"
fi
These are further helpful existing Q/As to refer to:
- BASH: Basic if then and variable assignment
- When to wrap quotes around a shell variable?
- Comparing integers: arithmetic expression or conditional expression
Command not found error in Bash variable assignment
You cannot have spaces around the =
sign.
When you write:
STR = "foo"
bash tries to run a command named STR
with 2 arguments (the strings =
and foo
)
When you write:
STR =foo
bash tries to run a command named STR
with 1 argument (the string =foo
)
When you write:
STR= foo
bash tries to run the command foo
with STR set to the empty string in its environment.
I'm not sure if this helps to clarify or if it is mere obfuscation, but note that:
- the first command is exactly equivalent to:
STR "=" "foo"
, - the second is the same as
STR "=foo"
, - and the last is equivalent to
STR="" foo
.
The relevant section of the sh language spec, section 2.9.1 states:
A "simple command" is a sequence of optional variable assignments and redirections, in any sequence, optionally followed by words and redirections, terminated by a control operator.
In that context, a word
is the command that bash is going to run. Any string containing =
(in any position other than at the beginning of the string) which is not a redirection and in which the portion of the string before the =
is a valid variable name is a variable assignment, while any string that is not a redirection or a variable assignment is a command. In STR = "foo"
, STR
is not a variable assignment.
Getting command not found error while comparing two strings in Bash
This is problem:
if [[$variable == $blanko]];
Spaces are required inside square brackets, use it like this:
[[ "$variable" == "$blanko" ]] && echo "Nichts da!" || echo "$variable"
Every command returns error in Rhel
Your path
has been messed up and you will have to readd anything needed to your path.
This can be done by issuing the following command
export PATH="$PATH:[directory to append to path]"
in the case of the commands not found this path might get you going on the right path
export PATH="$PATH:/bin"
This however does not keep the $PATH
value across reboots in which case the system file holding the path settings will need to be edited.
Bash script returning command not found, when setting the PATH
Found the issue. My path was actually /bash-course/scripts
and not /bash_course/scripts
I corrected the exported path under .profile
Thanks everyone
Bash alias: command not found
Run bash
and then try the command.
Alternatively, put it in ~/.bash_profile
which should be loaded automatically.
when command not found, bash `$?` return 0, not 127
I read the answer of @chepner in detecting command not found in bash script
and add the unset command_not_found_handle
at the first line has solved the matter
Related Topics
How to Include File in a Bash Shell Script
Fast Way to Get Image Dimensions (Not Filesize)
Linux Cmd to Search for a Class File Among Jars Irrespective of Jar Path
Comparing Two Unsorted Lists in Linux, Listing the Unique in the Second File
How to Shield a CPU from the Linux Scheduler (Prevent It Scheduling Threads Onto That Cpu)
How to Create a Directory and Give Permission in Single Command
Move Window Between Tmux Clients
How to Delete All Files Older Than 3 Days When "Argument List Too Long"
How to Enable Scrolling in Tmux Panels with Mouse Wheel
How to Know Which of the /Dev/Input/Eventx (X=0..7) Have the Linux Input Stream
Suppress Echo of Command Invocation in Makefile
Total Size of the Contents of All the Files in a Directory
Check Battery Level of Connected Bluetooth Device on Linux
Centos/Linux Setting Logrotate to Maximum File Size for All Logs
How to Have Tcpdump Write to File and Standard Output the Appropriate Data