I started writing shell scripts again, and I found myself in a situation where I often have to write a debugging echo to keep track of what the script is doing. Easy, as I usually did, was to write something like this:
#!/bin/bash
myVar = 'Erractic Nonesense'
echo "myVar: $myVar"
==> myVar: Erractic Nonesense
This worked fine and was pretty simple, but writing it for every variable that I wanted to track was tedious, and, as a person who thinks that having less code to create more materials, I wrote myself a function:
#!/bin/bash
dbg()
{
echo "$@: ${!@}"
}
myVar = 'Erractic Nonesense'
dbg myVar
==> myVar: Erractic Nonesense
This works fine for regular variables, but does not work for script arguments ($ 1, $ 2, etc.). Why?
==> $ ./myScript 123
#!/bin/bash
...
dbg 1
==> 1: 1
And also, how can this be circumvented?
EDIT
Thanks to Barmar, I now see why he behaves this way, but the second question remains.
EDIT 2
koodawg, . . , . EDIT 4
3
, EDIT 2 set +-x .
EDIT 4
, , . .
RX_INTEGER='^[0-9]+$'
DBG_SCRIPT_ARGS=( "$0" "$@" )
DBG_PADDING=" "
dbg()
{
args=$@
[[ $args =~ $RX_INTEGER ]] && res="${DBG_SCRIPT_ARGS[args]}" || res="${!@}"
printf "%s%s\`%s\`\n" "$args:" "${DBG_PADDING:$(((${#args}-1)%${#DBG_PADDING}))}"
}