Yuxuan
Yuxuan

Reputation: 71

How to get standard output from subshell?

I have a script like this?

command='scp xxx 192.168.1.23:/tmp'
su - nobody -c "$command"

The main shell didn't print any info. How can I get output from the sub command?

Upvotes: 0

Views: 1280

Answers (2)

fork0
fork0

Reputation: 3461

You can get all of its output by just redirecting the corresponding output channel:

command='scp ... '
su - nobody -c "$command" > file

or

var=$(su - nobody -c "$command")

But if you don't see anything, maybe the diagnostics output of scp is disabled? Is there a "-q" option somewhere in your real command?

Upvotes: 1

Alan Curry
Alan Curry

Reputation: 14711

You aren't actually running the scp. When you use the

VAR=value cmd ...

syntax, the VAR=value setting goes into the environment of cmd but it's not available in the current shell. The command after your -c is empty, or the previous value of $command if there was one.

Upvotes: 0

Related Questions