2

I'm wondering if it's possible to "extract" the first 5 lines of a textfile to a single variable (not an array)

for example:

head -5 test.txt >$variable (which of course doesn't work)

I'm trying to use zenity to display the first lines so I can confirm / cancel depending on the text displayed

zenity --question \
--text=$text

(other working solutions are of course appreciated...)

JoBe
  • 387
  • 5
  • 16

1 Answers1

3

It's as simple as

variable=`head -5 test.txt`
# or
variable=$(head -5 test.txt)

Looks like you are not well versed in shell scripting basics. Here's are nice guides:

Artem S. Tashkinov
  • 26,392
  • 4
  • 33
  • 64
  • Please note that the "backtick" notation you showed in the first example is [deprecated](https://unix.stackexchange.com/questions/126927/have-backticks-i-e-cmd-in-sh-shells-been-deprecated) now; you may want to remove that. Also, it is generally advisable to [quote](https://unix.stackexchange.com/questions/443989/whats-the-right-way-to-quote-command-arg) command substitutions (i.e. `variable="$(...)"`). – AdminBee Aug 10 '20 at 10:03
  • 2
    @AdminBee: There is no need to quote command substitutions when used for variable assignment. – jesse_b Aug 10 '20 at 14:35