4

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
dot
  • 705

2 Answers2

10

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.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • Being a noobie with bash - I didn't quite understand your explanation of the variable being exported from my environment. But I did substitute with a lowercase variable name and the problem went away. I will change all my vars to be lower case. THanks. – dot Feb 14 '18 at 20:43
  • If you run env, you'll see a list of variables that have already been set and are exported/visible to child processes. Using any of those names is risky. – Jeff Schaller Feb 14 '18 at 20:45
  • gotcha. trying to accept your answer but looks like i have to wait a few minutes. – dot Feb 14 '18 at 20:46
  • No problem; glad to hear you got to the bottom of it! – Jeff Schaller Feb 14 '18 at 20:48
4

PAGER is a commonly used environment variable (like EDITOR). Some examples of it being mentioned in man pages:

man (though it prioritises MANPAGER):

-P pager, --pager=pager
Specify which output pager to use. By default, man uses pager. This option overrides the $MANPAGER environment variable, which in turn overrides the $PAGER environment variable.

git:

-p, --paginate
Pipe all output into less (or if set, $PAGER) if standard output is a terminal. This overrides the pager.<cmd> configuration options (see the "Configuration Mechanism" section below).

If your script uses the variable in some other meaning than holding the name of the program to pipe output to, I'd suggest using another name just for clarity. Regardless, remember that the surrounding environment can pass in any variable, so if you want to make sure a variable is empty, you'll have to explicitly assign the empty string to it. Or unset it, if you want it unset.

Though this is a decision you'll need to make. Some users might appreciate the possibility of passing configuration items in through the environment.

ilkkachu
  • 138,973