1

If I try following:

varReturn=$(ls)
echo $varReturn

it shows me the correct output of the listed elements in the directory.

But if I try this one:

varReturn=$(/opt/vc/bin/tvservice -n)
echo $varReturn

it doesn't show me the expected output :/

My goal is to check if an HDMI Port is connected or not. It' very curious for me, why it works only for some commands.

I'm looking forward to getting some help here. I didn't figure out, what the problem is.

EDIT:

Now I've found another way and tried following:

varReturn=`tvservice -s`
echo $varReturn

this shows me the correct output:

enter image description here

But if I use another command, like this one:

varReturn=`tvservice -n`
echo $varReturn

It shows me no output at echo, but the output from the var (confusing). enter image description here

It still shows me the output if I use following code:

varReturn=`tvservice -n`
#echo $varReturn

The output is shown without the blank space.

7
  • 1
    Why not replace varReturn=$(ls)\necho $varReturn with ls, similalry just use /opt/vc/bin/tvservice -n instead of putting it into a variable and echoing the variable. Commented Feb 22, 2018 at 10:34
  • just run this command /opt/vc/bin/tvservice -n in command prompt manually and see if you get any output? Commented Feb 22, 2018 at 10:53
  • yes i get following output: [E] No device present Commented Feb 22, 2018 at 10:55
  • execute this command again and just check with $? and let me know. Commented Feb 22, 2018 at 10:57
  • It doesn't work with $ I also tried it with quote and double quote. This is damn confusing. Commented Feb 22, 2018 at 11:01

2 Answers 2

1

There is at least one problem with this code:

varReturn=$(/opt/vc/bin/tvservice -n)
echo $varReturn
#    ^ missing double quotes around this variable

Adding those quotes will ensure that the variable is passed as a single argument to echo. Otherwise, echo sees a list of arguments and outputs each one, separated by a space.

The next possible issue is that the command is outputting to standard error, rather than standard output, so it won't be captured by $() or the old-fashioned equivalent ` `. To correct this, try:

output=$(/opt/vc/bin/tvservice -n 2>&1)
#                                 ^ redirect standard error to standard output
echo "$output"
Sign up to request clarification or add additional context in comments.

Comments

1

When you execute a shell command like varReturn=$(/opt/vc/bin/tvservice -n) it will store the output to the variable only when the command executed successfully, else it will not hold any information because error/unsuccessful message will be redirected to standard error. Hence you have to redirected it to standard output like below:-

varReturn=$(/opt/vc/bin/tvservice -n 2>&1)

Now in both successful and unsuccessful execution case output will store in variable varReturn.

4 Comments

I agree that the solution might be to redirect standard error, but it is perfectly reasonable for a program to write to both standard output and standard error, whether or not it exits successfully.
Yes, I have implemented in some of my shell script which are working fine at the moment. I also faced the same issue what you are facing now. Based on my experience I am wring my answer.
Thank you very much, this works for me! The same solution as @TomFenech posted.
Submitted almost at the same time. Thanks anyway :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.