Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 '14 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 '14 at 16:36
What's the point, as opposed to just executing the file? – l0b0 Jun 21 '14 at 16:47
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


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.