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.

I have file.txt with command stored in one line (this command is valid when running in console) and I want to execute it in one line with sh like

cat file.txt | eval

what is missing? any protips?

and what if I have file with many commands (one for each line) and I want to execute only one command (one whole line)? my first idea is:

head -n5 | tail -n1 | eval
share|improve this question
    
possible duplicate of cat line X to line Y on a huge file –  HalosGhost Jun 21 at 16:34
    
Though there is a slight twist, what you're looking for appears to be very similar to extracting part of a file (which has been covered in several questions, e.g., here. –  HalosGhost Jun 21 at 16:36
    
What's the point, as opposed to just executing the file? –  l0b0 Jun 21 at 16:47

1 Answer 1

up vote 4 down vote accepted

eval does not read its command string from stdin.

eval "$(cat file.txt)"
# or easier, in bash
eval "$(<file.txt)"
# usually you cannot be sure that a command ends at the end of line 5
eval "$(head -n 5 file.txt)"

Instead of eval you can take source if the commands are in a file anyway:

source file

That does not work with choosing a part of the file, of course. That could be done by:

head -n 5 file.txt >commands.tmp
source commands.tmp
share|improve this answer

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.