How can I run a specific line as a command in a text file?

Try this:

sed -n 'line_num p' | bash

or, if the command does not contain whitespace,

"$(sed -n 'line_num p')"

If you're doing this interactively, you could write the line into the bash history:

history -s "$(sed -n 'line_num p')"

Then press Enter to insert the line into the readline buffer, and execute it; that gives you a chance to glance at the line before it's too late.


You can do...

{ head -n"$((NUM-1))"; IFS= read -r line; } </path/to/script >/dev/null
eval "$line"

That will get only the $NUM line from your script in the shell variable $line and then evaluate it as a command in the current shell.

Another way to do this could look like:

 </path/to/script sed "${NUM}q;d" >/tmp/"$$"
 . /tmp/"$$" ; rm /tmp/"$$"