Sample Header Ad - 728x90

Why does a 'pipe-to-awk' behave differently inside a function?

2 votes
1 answer
197 views
I'm using alsa to fiddle around with the volume on a Bluetooth speaker I have connected. It's a 1-speaker setup; just something to provide some background. I can 'get' the volume setting fm the CLI as follows:
$ amixer sget Master
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 22938 [35%] [on]
  Front Right: Playback 22938 [35%] [on]
I can 'set' the volume fm the CLI as follows:
$ amixer sset Master 50%
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 65536
  Mono:
  Front Left: Playback 32768 [50%] [on]
  Front Right: Playback 32768 [50%] [on]
I don't need all this *verbosity*, so I decided to create a function, and two aliases in my ~/.bashrc file to reduce that. The function is not working as I expected: From the CLI, this works fine:
$ amixer sget Master | awk -F"[][]" '/Right:/ { print $2 }'
35%
But when I put this into my function statement in ~/.bashrc, it works ***differently***:
# functions & aliases for alsa mixer/volume control
function vol-get() {
   amixer sget Master | awk -F"[][]" '/Left:/ { print $2 }'
}
export -f vol-get

alias vol-up='amixer sset Master 5%+ > /dev/null && vol-get'
alias vol-dn='amixer sset Master 5%- > /dev/null && vol-get'
Re-reading ~/.bashrc & running the vol-get function yields the following:
$ . ~/.bashrc
$ vol-get
  Front Left: Playback 22938 [35%] [on]
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ?? what happened here ??
$ vol-up
  Front Left: Playback 26215 [40%] [on]
$
So - I'm *close*, but I don't understand why my | awk ... behaves differently inside the function than it does on the command line. Can someone explain and correct this? --- ### Edits for answers to some of the Questions raised in the comments: #### EDIT #1:
$ command -V vol-get
vol-get is a function
vol-get ()
{
    amixer sget Master | awk -F"[][]" '/Left:/ { print $2 }'
} 

$ awk --version
awk: not an option: --version
$ man awk
# reveals that I am actually calling mawk, Version 1.3.4
#### EDIT #2: RE: non-printing characters; note this is a partial c&p from the output (i.e. verbatim):
$ cat -vet ~/.bashrc
   ...
   function vol-get() {$
       amixer sget Master | awk -F'[][]' '/Left:/ { print $2 }'$
   }$
   ...
Asked by Seamus (3772 rep)
Oct 19, 2024, 07:21 AM
Last activity: Oct 20, 2024, 09:46 PM