Bash – How to Run a Specific Line as a Command in a Text File

bashcommand line

Sometimes I need to exec a single command which is in a shell script.

I already know sed -n 'line_num p' can print that line. But how can I exec that printed out specific line as a command?

Best Answer

Try this:

sed -n 'line_num p' | bash

or, if the command does not contain whitespace,

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