are you convinced this script does not work as expected?

The fix

You have 2 equally viable options:

  1. Use -gt
if [ $# -gt 0 ]
  1. 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.

Leave a Comment

Your email address will not be published.

Scroll to Top