The fix
You have 2 equally viable options:
- Use
-gt
if [ $# -gt 0 ]
- Use double brackets
[[
(Does a lexicographic comparison but will work for this case)
if [[ $# > 0 ]]
Why?
When you did if [ $# > 0 ]
the >
was treated like an output redirection command similar to echo "foo" > file.txt
. You might notice you have created a file named 0
someplace after executing:
if [ $# > 0 ]
When deciding between using [...]
or [[...]]
many find the answer is to use double brackets
Getting fancy
Now if what you’d really like to do is write a script that gives a default value to the u
variable if none is provided by the first argument I would recommend using a neat bash syntax trick for implementing default values
u=${1:-${USER}}
CLICK HERE to find out more related problems solutions.