1

Consider the following shell script

val=($ls)

The ls does not not give any shell text output. Now, how do we get output text on screen while the command is being executed?

I can print the value of val to get the output, but using echo is not the point. So, using the following line is not the case

echo $val

So, in nutshell, how do I get the output of current command being executed in shell simultaneous as if you were executed the command by itself?

  • Your assignment is wrong, it would be val="$(ls)" and then echo $val – GypsyCosmonaut Jul 8 '17 at 10:03
2

You can get the shell to echo everything it is doing, by running the command thus:

sh -x yourscript

Or you can add this as the first command in the script:

set x

It can get a bit too verbose, though. It's OK for debugging, but if you want selective output it would be best to do it yourself with carefully places echo commands.

  • 1
    That is not going to show the output of commands - it will only show which commands are being run. – l0b0 Jul 8 '17 at 8:39
  • It showed the output of ls in the original questioner's example. I tried it. Which is what he was asking about. Presumably because the output of ls actually forms part of the subsequent command. – Bob Eager Jul 8 '17 at 13:39
2

You can tee commands to send a copy of standard output to a file, and you can use the current terminal as that output file. As you can see in the following, it will print twice if you don't use the output for anything, and once if standard output is swallowed by doing something to it:

$ echo foo | tee -a /dev/tty
foo
foo
$ echo foo | tee -a /dev/tty | grep bar
foo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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