0

So I'm not sure why this is.. I know ${LINENO} normally contains the line in the script, and BASH_LINENO goes based on input from BASH_SOURCE as an array

$ (echo hi;declare -p LINENO BASH_LINENO;echo -e "My LINENO\tis\t${LINENO}\nMy BASH_LINENO\tis\t${BASH_LINENO[@]}")
hi
declare -- LINENO="34"
declare -a BASH_LINENO=()
My LINENO   is  34
My BASH_LINENO  is

But why when I am working interactively is LINENO coming back as 30+ then incrementing every time I run echo (note like when I am saying interactive I am meaning like this is an activate shell with pty attached and fd 0, 1, and 2 being there). Can anyone explain to me why LINENO is at 33, 34, 35 etc when running multiple times?

2
  • 1
    start a new shell, run echo $LINENO, it shows 1 since that's the first input line. Run it again, it shows 2 since that's the second input line, etc.
    – ilkkachu
    Commented Feb 16 at 12:13
  • 1
    Aha, thanks yeah that was it, this could be useful for history expansion if stored PROMPT_COMMAND or PS1 to help you count backwards for history expansion...!
    – linuxgeek
    Commented Feb 16 at 12:39

1 Answer 1

1

This is what bash(1) says (bold mine):

LINENO

    Each time this parameter is referenced, the shell substitutes a decimal number representing the current sequential line number (starting with 1) within a script or function. When not in a script or function, the value substituted is not guaranteed to be meaningful. If LINENO is unset, it loses its special properties, even if it is subsequently reset.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .