Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do you store, and later execute, a command with redirection and and a pipe? (And is it the pipe, or the redirection, or both, that's causing my problem?)

I tried to print the urxvt version in a script, as well as echo the actual command.

The command I use to get the version is urxvt -help 2>&1 | head -n 2 - for some reason, urxvt prints help to stderr; as for the head, only the two first rows contain version and configuration data.

In the shell, it works great, but, in the script (isolated the problem, below), it seems the third line loses its head:

#!/bin/bash

VER_URXVT='urxvt -help 2>&1 | head -n 2'
echo $VER_URXVT
$VER_URXVT
share|improve this question
1  
You probably meant to use backticks: ` instead of single quotes: ' - their meaning is quite different. –  peterph Dec 22 '12 at 22:45
    
@RahulPatil: That doesn't seem to work. –  Emanuel Berg Dec 22 '12 at 22:48
    
@peterph: What I can see, that doesn't work. –  Emanuel Berg Dec 22 '12 at 22:49
    
You don't need the third line if you use backticks. –  ire_and_curses Dec 22 '12 at 22:56
    
echo $VER_URXVT | bash –  Rahul Patil Dec 22 '12 at 23:00
show 5 more comments

4 Answers

up vote 1 down vote accepted

You need to use the eval expression

#!/bin/bash

VER_URXVT='urxvt -help 2>&1 | head -n 2'
echo $VER_URXVT
eval $VER_URXVT

From the man page eval

          The  args  are read and concatenated together into a single com-
          mand.  This command is then read and executed by the shell,  and
          its  exit status is returned as the value of eval.  If there are
          no args, or only null arguments, eval returns 0.
share|improve this answer
    
Thanks a lot, this is exactly what I wanted. Thanks not only for your solution, but also for you taking your time understanding the question. –  Emanuel Berg Dec 22 '12 at 23:03
add comment

Use:

VER_URXVT=`urxvt -help 2>&1 | head -n 2`

which is the same as:

VER_URXVT=$(urxvt -help 2>&1 | head -n 2)

to get the first two lines of urxvt help into VER_URXVT.

If you want to evaluate the command in shell variable, you should use eval:

VER_URXVT='urxvt -help 2>&1 | head -n 2'
eval $VER_URXVT
share|improve this answer
add comment

I keep on thinking functions are better than evaluing variables

#!/bin/bash

version() { uname -a;}
wm() { head -n1 <(openbox --version);}
for f in $(compgen -A function)
do declare -f $f | sed '1,2d; $d'
   $f
done
share|improve this answer
    
Hi Watael , nice info, if possible put comment :) –  Rahul Patil Dec 24 '12 at 11:11
add comment

don't ever use eval unless you really know what will be evaled.

instead use a function.

#!/bin/bash

ver_urxvt() { urxvt -help 2>&1 | head -n 2;}
declare -f ver_urxvt | sed '1,2d; $d'
ver_urxvt

but it's silly, as it is a waste of time to print those informations.

if you want to debug your script use the -x option of the set builtin

share|improve this answer
1  
You can take a look at the script in which I need this. So you see, individual functions is not an option. As for what you think is silly or not, that's irrelevant as this is a Q&A site. Downvote. –  Emanuel Berg Dec 22 '12 at 23:23
    
@UlrichDangel: When I said I needed it, that referred to a general solution, as opposed to individual functions for all commands. As for eval, I don't know how that differs from backticks/$(), but if you care to explain why it is considered unsafe, sure, I'll change it. –  Emanuel Berg Dec 22 '12 at 23:35
    
You ask. I answer. The answer is an opinion on how things should be handled, instead of your non-working, and counter-productive idea. Q&A can also show what are best practices, and tell what are worst. btw: no "hi", no "please", no "thanks"... do you think we owe you something? learn to live with others, that'll improve your coding too. –  watael Dec 23 '12 at 0:14
    
What on earth are you talking about?! 1. The script works exactly as intended with the eval solution, 2. ... which I thanked, if you read the comment above, 3. the moderators frequently remove greetings, so I've stopped using them, although I fail to see how that's a big issue, and 4. do you actually expect me to thank you after calling my question silly, and my wish to print information "a waste of time"? –  Emanuel Berg Dec 23 '12 at 0:26
    
ah, ok! you misunderstood me: you don't waste my time, you uselessly occupy the resources of the machine for printing such informations. –  watael Dec 23 '12 at 0:41
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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