Bash script: incorrect argument value being set

bashgetoptsvariable

Problem

I have a script that accepts a few different (optional) command line arguments. For one particular argument, I'm getting the value "less" appear but I don't know why.

Bash Code

while getopts ":d:f:p:e:" o; do
  case "${o}" in
        d)
            SDOMAIN=${OPTARG}
            ;;
        f)
            FROM=${OPTARG}
            ;;
        p)
            PAGER=${OPTARG}
            ;; 
        e)
            DESTEXT=${OPTARG}
            ;;                       
        *)
            show_usage
            ;;          
    esac
done

source "./utils.sh"
test #test includes

echo "$SDOMAIN is the sdomain"
echo "$FROM is the from"
echo "$PAGER is the pager"
echo "$DESTEXT is the extension"

exit

Output

When I run my script, this is what I see:

lab-1:/tmp/jj# bash mssp.sh -d testdomain.net         
Utils include worked!                                       
testdomain.net is the sdomain                                               
 is the from                                                                    
less is the pager                                                               
 is the extension                                 

I can't see why I'm getting the "less" value in pager. I was hoping to see empty string. If you can see my bug, please let me know. I've been looking at this too long.

Best Answer

Your script never sets PAGER, but that variable is likely exported from your current environment; check with declare -p PAGER.

I would recommend using a different variable name inside your script; this is why there's a general recommendation against using upper-case variables as your own.

Related Question