Outcome
Filter output the variables displayed using set or declare to show only lowercase named shell variables which is the convention I use for setting shell variables while testing.
Example
I set these:
$ declare -A pid=()
$ a=DHCP
$ something=test
I want the output to look like this:
$ lvar
a=DHCP
pids=()
something=test
I came up with this awk command for the lvar function:
declare | awk -F'=' '$1 ~ /^[a-z]+$/'
This works perfectly when run from the command line. However, as a function sourced in and run, I get an error which I think is caused by awk's field parameter $1 being interpreted as a bash function parameter.
$ vi func
function lvar() {
declare |awk -F'=' '$1 ~ /^[a-z]+$/'
}
Running lvar gives this error:
$ lvar
awk: cmd. line:1: ~ /^[a-z]+$/
awk: cmd. line:1: ^ syntax error
What am I doing wrong here?
$ declare -p -f lvar
lvar ()
{
declare | awk -F'=' '$1 ~ /^[a-z]+$/'
}
Update 1
My original approach with an awk command defined as a bash function, does actually work - only on my Linux box and not on my Mac where I originally defined it.
I'll have to figure out the macOS issue and post an update to you all here in due course.
Many thanks for your time :)
Update 2
Okay, I checked if the bash shopt options on my Linux vs macOS were different but that wasn't it.
I opened iTerm on Mac and the function behaves as expected but shows no output in Terminal.app.
Very odd that its specific to some setting in Terminal.app. I'm at a loss.
awkcode? Check withdeclare -p -f lvar. Also note that$1inawkis not a positional parameter, but the value of the first field of the current record.awksyntax error,$1is not "interpreted as a bash variable". You have to present a reproducible example. Yourawkcommand is valid but has no argument and just waiting for stdin.declare |part. Updated.function lvar() { foo }- get rid of the nonportable, redundantfunctionkeyword and just dolvar() { foo }.