Bash cannot store info from blkid into a variable

331 views Asked by At

I am trying to store the output of my parsed string into a variable. When I run the command like this I get the correct output on the terminal output:

$ blkid | grep -oP '(?<=LABEL=").+?(?=")'
MYUSB_NAME

However, when I try to store it in a variable like this :

$ USB_NAME={blkid | grep -oP '(?<=LABEL=").+?(?=")'}; echo $USB_NAME;
(blank)

It just shows blank space. Does anyone know what I am doing wrong here?

Here is the full blkid output:

$ blkid
/dev/sda1: UUID="88dd2cc5-0a4f-4477-b678-159613aaf920" TYPE="ext4"
/dev/sda2: UUID="ggxkwK-DZfA-DnTF-dWbn-msx6-egfo-GuGIuS" TYPE="LVM2_member"
/dev/mapper/vg_baldy1-lv_root: UUID="76ab63ba-e8c4-4040-9533-5c8562f739d4" TYPE="ext4"
/dev/mapper/vg_baldy1-lv_swap: UUID="462a170b-5e09-4b98-98ce-09e0a24009ab" TYPE="swap"
/dev/mapper/vg_baldy1-lv_home: UUID="c38c0b9a-1049-4e9f-8800-d68d2e450929" TYPE="ext4"
/dev/sdb1: LABEL="MYUSB_NAME" UUID="BF05-9FC6" TYPE="vfat"
1

There are 1 answers

0
codeforester On

{ ... } is used for grouping commands. Use $() for command substitution:

USB_NAME=$(blkid | grep -oP '(?<=LABEL=").+?(?=")')

See: