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
  • 397
  • 5
  • 17

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:

  • Please note that the "backtick" notation you showed in the first example is deprecated now; you may want to remove that. Also, it is generally advisable to quote 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