Bash – access Nth line number of standard output

bashefficiencykeyboard shortcutsstdoutterminal

Assuming a script that outputs a list of files:

$> bash someScript.sh
path/to/some/file
path/to/the/file/i/want/to/work/with
path/to/yet/another/file

Now I want to have the second file-path as parameter for another command, e.g. vim. Is there a way to directly access it?

And I want to point out that I do not necessarily want to access the second file, but the next time it could be the third or the 27th. I want to be able to select that Nth line as easily as possible.

Right now I do mouse-selecting and insert by middle-clicking or type the path with tab-completion. Now I wonder if there is an easier way.

Problem with my own solution is though that I would have to edit all my scripts this way. It would be fun if there was a more general solution to this issue, that would work with any kind of command, e.g. find.

Best Answer

Sure.

bash someScript.sh | sed -n '2 p'

will filter your output and just print the second line of it. To make that a parameter to vim:

vim "$(bash someScript.sh | sed -n '2 p')"