Operating System - HP-UX
1845948 Members
2263 Online
110250 Solutions
New Discussion

Shell variable containing pipe

 
SOLVED
Go to solution
Stephen Mudd
Occasional Contributor

Shell variable containing pipe

Hello,

Can someone explain why a remote shell interprets a pipe, but a local shell doesn't?
e.g.
# COMMAND='echo hello | grep hell'
# remsh other_host $COMMAND
hello
# $COMMAND
hello | grep hell

Why does the local shell execute the echo, but not react to the pipe?
What syntax should I use to get the same result as the remsh?
7 REPLIES 7
Stefan Farrelly
Honored Contributor

Re: Shell variable containing pipe

your command worked. Works fine for me with a pipe in a variable or not.

You did the remote command;

echo hello | grep hell

and hell is part of hello so it echoed back hello. This is normal behaviour.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Stanimir
Trusted Contributor

Re: Shell variable containing pipe

Normal behavior! :)

Use:
#COMMAND=`echo hello|grep hell`
#echo $COMMAND

Regards,STAN
Todd McDaniel_1
Honored Contributor

Re: Shell variable containing pipe

/agree

On your local host, the echo command only regurgitates what you give it in quotes, it will not treat it as an executable command... merely as plain text.

You would have to pass the variable to a script or one-liner... or change the quotes to back tics... as below.

You will need to change it to back tics..the one next to the 1 key....


# COMMAND=`echo hello | grep hello`
# echo $COMMAND
hello
Unix, the other white meat.
Todd McDaniel_1
Honored Contributor

Re: Shell variable containing pipe

Its amazing to me 3 of us posted at the exact same time LOL... I didnt copy the guy above me LOL...so please dont penalize me on points!!

of course zero for this post of course!
Unix, the other white meat.
Stanimir
Trusted Contributor

Re: Shell variable containing pipe

Sorry Tod!
Really your answer is more complete
and usefull.

Regards,Stan
Rodney Hills
Honored Contributor
Solution

Re: Shell variable containing pipe

It has to do when the shell is interpreting the command.

On the local system
$COMMAND
is replaced with the the contents of COMMAND and the echo command is executed. But the "|" has no meaning except on the command line you typed. It is not processed by the shell.

If you wanted $COMMAND to recognize the "|", then use the "eval" shell command (see man sh-posix).
eval $COMMAND

Then the shell will rescan the command line a second time, see the "|" and do the pipe operation.

The reason it works on the remsh is the same situation. When $COMMAND is run on the remote host, the remote host shell rescans the command line and sees the "|" as its special meaning.

HTH

-- Rod Hills
There be dragons...
Stephen Mudd
Occasional Contributor

Re: Shell variable containing pipe

Thanks for your replies. I should have realised what was going on without making myself look like a complete novice. Never mind. We all have off days occasionally.

Using "eval" is preferable to using "echo $(command)" or backquotes because it retains the formatting, e.g.

# COMMAND1="/usr/sbin/lanscan |cut -c1-8,24-80"
# COMMAND2=$(/usr/sbin/lanscan |cut -c1-8,24-80)
# echo $COMMAND2
Hardware Crd Hdw Net-Interface NM MAC HP-DLPI DLPI Path In# State NamePPA ID Type Support Mjr# 0/0/0/0 0 UP lan0 snap0 1 ETHER Yes 119 0/4/0/0 1 UP lan1 snap1 2 ETHER Yes 119 0/6/0/0 2 UP lan2 snap2 3 ETHER Yes 119 0/2/0/0 0 UP ixe1 5 X25 No 77 0/2/0/0 0 UP ixe2 6 X25 No 77
# eval $COMMAND1
Hardware Crd Hdw Net-Interface NM MAC HP-DLPI DLPI
Path In# State NamePPA ID Type Support Mjr#
0/0/0/0 0 UP lan0 snap0 1 ETHER Yes 119
0/4/0/0 1 UP lan1 snap1 2 ETHER Yes 119
0/6/0/0 2 UP lan2 snap2 3 ETHER Yes 119
0/2/0/0 0 UP ixe1 5 X25 No 77
0/2/0/0 0 UP ixe2 6 X25 No 77
# remsh other_host $COMMAND1
Hardware Crd Hdw Net-Interface NM MAC HP-DLPI DLPI
Path In# State NamePPA ID Type Support Mjr#
0/0/0/0 0 UP lan0 snap0 1 ETHER Yes 119
0/5/0/0 2 UP lan2 snap2 2 ETHER Yes 119
0/10/0/0 1 UP lan1 snap1 3 ETHER Yes 119
0/8/0/0 0 UP ixe1 5 X25 No 79
0/8/0/0 0 UP ixe2 6 X25 No 7