Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have following command:

usb_soundcard_sink=$(pactl list short sinks | grep "alsa_output" | awk '{ print $2 }' | tail -n1)

output of this command is:

alsa_output.pci-0000_00_1b.0.analog-stereo

And this is another command that find index number:

var=$(pactl list short sinks | awk '$2=="alsa_output.pci-0000_00_1b.0.analog-stereo" {print $1}')

output:

0

But I want pass the "usb_soundcard_sink" variable instead of hard coded value i.e. "alsa_output.pci-0000_00_1b.0.analog-stereo" in above command. b'coz value of "usb_soundcard_sink" variable may change dynamically.

Also I tried the following:

var=$(pactl list short sinks | awk '$2=="$usb_soundcard_sink" {print $1}')

But it is not working

so how can I pass value of "usb_soundcard_sink" variable to the above command

share|improve this question

2 Answers 2

up vote 4 down vote accepted

To use bash variables inside your awk, you have to use -v like this:

pactl list short sinks | awk -v myvar="$usb_soundcard_sink" '$2==myvar {print $1}'
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^      ^^^^^

Example

$ a=3
$ awk -v myvar="$a" 'BEGIN{print myvar}'
3
share|improve this answer

I found that this works too

line=myfile.dat
f=8
s=11
echo $line | awk '{print $'$f',$'$s'}'

Here I have a file called myfile.dat piped into awk which spits out column 8 and 11.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.