-1

so I get the first file in directory by commanding "ls | sort -n | head -1" is there any way to store this output into a variable shell script?

  • Possible duplicate of [How to set a variable to the output of a command in Bash?](https://stackoverflow.com/q/4651437/608639), [Bash script store command output into variable](https://stackoverflow.com/q/9768228/608639), etc. – jww Sep 20 '18 at 16:30

1 Answers1

-1

output of a command can be stored to a variable using backquotes(`). For example.

var=`ls | sort -n | head -1`
echo $var

Does this answer your question?

Sourav Gulati
  • 1,359
  • 9
  • 18
  • Even Posix-Shell has `$(....)`, so I suggest using this one instead of the (deprecated and less versatile) backquotes. – user1934428 Sep 20 '18 at 08:03
  • Ya this can be used as well. Both approaches are correct – Sourav Gulati Sep 20 '18 at 08:52
  • Is there anything wrong with the answer? Why has it been downvoted? – Sourav Gulati Sep 21 '18 at 06:52
  • I don't know who downvoted it. My guess is that it is because of the use of a deprecated features (backquotes) in the answer, because people who will use this answer and maybe pickup the habit of also using this. At least this is the only negative point I can see. Maybe you could bless your backquotes by mentioning, that your solution would even work with csh ;-) – user1934428 Sep 21 '18 at 12:02