Need to assign the contents of a text file to a variable in a bash script

前端 未结 3 1817
离开以前
离开以前 2020-12-15 03:40

I am very new to making bash scripts, but my goal here is to take a .txt file I have and assign the string of words in the txt file to a variable. I have tried this (no clue

相关标签:
3条回答
  • 2020-12-15 03:53

    The $() construction returns the stdout from a command.

    file_contents=$(cat answer.txt)
    
    0 讨论(0)
  • 2020-12-15 04:00

    In bash $(< answer.txt) is a builtin shorthand for $(cat answer.txt)

    I suspect you're running this print:

    NAME  
        run-mailcap, see, edit, compose, print − execute programs via entries in the mailcap file
    
    0 讨论(0)
  • 2020-12-15 04:04

    You need the backticks to capture output from a command (and you probably want echo instead of print):

    file1=`cat answer.txt`
    echo $file1
    
    0 讨论(0)
提交回复
热议问题