Creating an array from a text file in Bash

前端 未结 6 1902
小鲜肉
小鲜肉 2020-11-22 10:25

A script takes a URL, parses it for the required fields, and redirects its output to be saved in a file, file.txt. The output is saved on a new line each time a fie

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-22 11:09

    This answer says to use

    mapfile -t myArray < file.txt
    

    I made a shim for mapfile if you want to use mapfile on bash < 4.x for whatever reason. It uses the existing mapfile command if you are on bash >= 4.x

    Currently, only options -d and -t work. But that should be enough for that command above. I've only tested on macOS. On macOS Sierra 10.12.6, the system bash is 3.2.57(1)-release. So the shim can come in handy. You can also just update your bash with homebrew, build bash yourself, etc.

    It uses this technique to set variables up one call stack.

提交回复
热议问题