Bash: Assigning variable to "snap refresh --list" result, leaves variable empty

Hi there!

Hopefully this is the right forum for a question regarding “snap refresh”. In bash (script), I’d like to assign the output of “snap refresh --list” to a variable that can be interrogated by the script (ie determine whether there are any pending snap app updates and take further action). In an overly simplified view of the script:

myVar=$(snap refresh --list) #Outputs "All snaps up to date." to STDOUT
echo $myVar                  #Outputs "" to STDOUT

Running the above, the assignment of the variable (line #1) publishes to STDOUT (“All snaps up to date.”) but the value of the variable remains empty. My expectation is the variable should be assigned a value and nothing is published to STDOUT during the assignment. Only when when the script calls: echo $myVar (ie line #2) will the anything be published to STDOUT.

I do get the expected outcome on other “snap” commands (such as "snap list --all), which makes me wonder if the “snap refresh” command is different somehow. Is this the expected behaviour? If so - can anyone help me understand why my variable is blank but there is output generated to STDOUT and how I may get the desired result (ie an assigned variable in my script)?

Many thanks!!

it might write to STDERR … try merging STDERR and STDOUT like:

$ var="$(LANG=C snap refresh --list 2>&1)"
$ echo $var
All snaps up to date.

Many thanks! That works perfectly! :slight_smile: It never occurred to me to redirect output