-bash: [: =: unary operator expected. when no parameter given [duplicate] - bash

This question already has answers here:
How to use double or single brackets, parentheses, curly braces
(9 answers)
Bash script error [: !=: unary operator expected
(2 answers)
Closed 5 years ago.
I have my shell script, myscript.sh below
#!/bin/sh
if [ $1 = "-r" ]; then
echo "I am here"
fi
If I run with . myscript.sh -r, it works well with message I am here.
But if I just run with . myscript.sh, it complaints
-bash: [: =: unary operator expected
What's missing in my script?

You would need to add quotes around $1.
if [ "$1" = "-r" ]; then
echo "I am here"
fi
When $1 is empty you are getting if [ = "-r"] which is a syntax error.

You have missed the quotes:
if [ "$1" = "-r" ]; then

Related

how to detect if a string ends with " in bash [duplicate]

This question already has answers here:
Difference between single and double square brackets in Bash
(7 answers)
How can I escape a double quote inside double quotes?
(9 answers)
Closed 6 months ago.
I have a problem with bash, I'm trying to detect strings in a file with bash with the file looking like
cnlog "Hello World!"
When i try the [ $variable = *" ] in bash it doesn't work and throws out an error
basilc.sh: line 13: unexpected EOF while looking for matching `"'
basilc.sh: line 16: syntax error: unexpected end of file
the code of the bash file is
char=""
while IFS='' read -n1 c; do
if [ $char = '"' ] || [ $char = *" ]
then
echo "STRING FOUND"
char=""
pwd
fi
echo "$char"
done < $1
Please help

Bash return [: =: unexpected operator error [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 1 year ago.
I just do a simple task,input a bc then if c is "+" it will print total of a+b
So i do this
echo "$a $b $c = "
if [ $c = "+" ]; then
echo $(($a + $b))
fi
And it return simple.sh: 3: [: =: unexpected operator
What is going on?? Please help, thank a lot, im so confuse now
If $c is unset or set to nothing then the expression would expand to: [ = + ] which would cause this problem.
Always quote your parameter expansions as the result will be unexpected otherwise:
[ "$c" = "+" ]
Unquoted parameter expansions will undergo word splitting and pathname expansion.
Whenever writing shellscripts it always a good idea to use the shellcheck linter, an online version is available at https://www.shellcheck.net/

"[0: command not found" in Bash [duplicate]

This question already has answers here:
How to use double or single brackets, parentheses, curly braces
(9 answers)
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
I am trying to get the array in the while-loop and need to update the value in array too.
Below is my code what I have tried. I get this error [0: command not found
#!/bin/bash
i=0
while [$i -le "{#myarray[#]}" ]
do
echo "Welcome $i times"
i= $(($i+1)))
done
How do I fix this?
Need a space after [ and no space before or after = in the assignment. $(($i+1))) would try to execute the output of the ((...)) expression and I am sure that's not what you want. Also, you are missing a $ before the array name.
With these things corrected, your while loop would be:
#!/bin/bash
i=0
while [ "$i" -le "${#myarray[#]}" ]
do
echo "Welcome $i times"
i=$((i + 1))
done
i=$((i + 1)) can also be written as ((i++))
it is always better to enclose variables in double quotes inside [ ... ]
check your script through shellcheck - you can catch most basic issues there
See also:
Why should there be a space after '[' and before ']' in Bash?
How to use double or single brackets, parentheses, curly braces
Command not found error in Bash variable assignment
Using [ ] vs [[ ]] in a Bash if statement

korn shell error [: missing `]'

I am learning Korn shell which is based on Bourne shell. Below is my really simple code.
read ab
if [ $ab = "a" || $ab = "A" ] ; then
echo hi
fi
For some reason || operator is giving me the error:
[: missing `]'
a: command not found
The correct way to write your if condition is:
read ab
if [ "$ab" = "a" ] || [ "$ab" = "A" ]; then
echo hi
fi
With [ ... ], it is essential to put the variables in double quotes. Otherwise, shell will fail with a syntax error if the variables expand to nothing or if their expansion contains spaces.
See also:
Why should there be a space after '[' and before ']' in Bash?
How to use double or single brackets, parentheses, curly braces
BashFAQ - What is the difference between test, single, and double brackets?
If you use ksh or a modern bash you can use the non-standard [[ ... ]] instead of [ ... ].
This has two benefits:
You can use || inside [[ ... ]]
Variable expansions do not need quotes.
This makes it safe and shorter to write
[[ $ab = a || $ab = A ]]

Shell spacing in square brackets [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 4 years ago.
As a beginner, I did not find answered anywhere, the rules about spacing (grammar) and parsing.
For example.
Can I do
if [$# -eq 2] ;
then
llll
fi
or must I always have a blank or two between the objects, as
if [ $# -eq 2 ] ;
then
llll
fi
and the second related question is about the difference between
if [[ $# -eq 2 ]] ;
then
wafwaf
fi
The concern I have is about spacing before/after [, ].
No searching has provided me with a set of rules.
Spaces are required after [ and before ].
[ is actually the name of a command, an alias for test. It's not a special symbol, it's just a command with an unusual name.
$ help '['
[: [ arg... ]
Evaluate conditional expression.
This is a synonym for the "test" builtin, but the last argument must
be a literal `]', to match the opening `['.
Because it's an ordinary command name and not a special character, a space is required after the [. If you omit the space and write [foo the shell will search the $PATH for a command named [foo.
$ [ foo = foo ] && echo true
true
$ [foo = foo] && echo true
[foo: command not found
For readability's sake, [ expects its last argument to be exactly ]. Being an ordinary command-line argument, ] must have a space before it. If there's no space then the bracket will become the last character of the previous argument, and [ will complain about its last argument not being ].
$ [ foo = foo]
bash: [: missing `]'
$ [ foo = 'foo]'
bash: [: missing `]'
[[ is a bash enhancement with more features than [, namely saner handling of unquoted variable names. It requires the a space on both ends, same as [. However [[ is in fact special shell syntax and is parsed differently. It's not an "ordinary command" the way [ is.
For a detailed explanation of the difference between [ and [[, see:
What's the difference between [ and [[ in Bash?

Resources