$LINENO no show

$LINENO is bash "system" variable but I cannot get any output trying to use it.

Am I missing something fundamental?

Here is the code snippet

13)
 247                echo "TEMPLATE Option 13"
 248
 249 # *** BEGIN DEBUG BLOCK ***
 250 last_cmd_arg=$_  # Save it.
 251
 252 echo "At line number $LINENO, variable \"v1\" = $v1"
 253 echo "Last command argument processed = $last_cmd_arg"
 254 # *** END DEBUG BLOCK ***
 255
 256 pause 






And here is the output in terminal

TEMPLATE Option 13
At line number , variable "v1" = 
Last command argument processed = 
Press [Enter] key to continue...




We have no idea what you are missing. But, there are lots of things we are missing...

  1. What operating system are you using?
  2. What shell are you using?
  3. What is the first line of the script you are running?
  4. What command did you use to invoke the script you are running?
1 Like

Are you sure, that the code being executed is inside a bash function? From my understanding of the bash man page, LINENO is not defined outside of functions.

From here: Bash Reference Manual: Bash Variables

Example script

#!/bin/bash
echo "arg count = $#"
while [ $# -gt 0 ]
do
    echo "$1 on $LINENO "
    shift
done

exit

output:

$ ./t.shl 1 2 3
arg count = 3
1 on 5
2 on 5
3 on 5

So, I'm confused.
According to the maintainers of bash and my Cygwin version of bash, the LINENO variable always exists. So, until Annacreek can tell us the information Don Cragun asked, we cannot know what shell is being used. I would guess the shell is not bash, if in fact the problem line of code actually gets executed when the script runs.

You are right! LINENO also works well outside a function.

As for finding out whether or not you are running bash, there are several ways. First of all, you are running the script, so I would say you know what you are using. There is no magic in this.

Aside from this, you could output the variable BASH_VERSION (which would be empty, unless someone malevolently sets it explicitly). You can also trigger a syntax error in the script; if it is bash, the error message will contain the word "bash".

This isn't just bash ... It was in ksh before 1988, and the latest POSIX standard says the following about the LINENO variable:

So, I guess we should also ask for the output from the command:

grep LINENO script_pathname

hoping to verify that the script doesn't assign any value to LINENO and doesn't use unset with LINENO as an operand.

1 Like

'$LINENO is bash "system" variable but I cannot get any output trying to use it."

OK, I did not say "I am using bash ".

Yes, being a geenie I thought
# denotes comments, apparently not.

So I moved
#!/bin/bash back as first line in file

Here is a code snippet :

czechlist_DEBUG(){
  64 echo "czechlist_DEBUG "
  65 echo "Passed parameter #1 is $1"
  66 echo "Passed parameter #2 is $2"
  67 echo "Passed parameter #3 is $3"
  68 echo "Passed parameter #4 is $4"
  69
   70 echo "arg count = $#"
initialize while "count" 

   71 while [ $# -gt 0 ]
  72 do
  73     echo "$1 on $LINENO " 

 echoes "first" parameter "found" , so the construct 

is 

      while 

      do 

     ...
     done 

??

 
 
  74     shift
  75 done
  76 echo " DONE line output "
  77
  78 pause 


And here is an output , getting there:

czechlist_DEBUG 
Passed parameter #1 is DEBUG USB 
Passed parameter #2 is lsusb
Passed parameter #3 is test par 2
Passed parameter #4 is test par 3
arg count = 4
DEBUG USB  on 73 
lsusb on 73 
test par 2 on 73 
test par 3 on 73 
 DONE line output 
Press [Enter] key to continue...