Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0125· openaiHugging Face Suzhen/CodeChat

Linux bash, store contenue of file to variable

  1. User

    Linux bash, store contenue of file to variable

  2. Assistant

    To store the contents of a file in a Linux bash script to a variable, you can use the cat command followed by command substitution. Here's an example:

    file_contents=$(cat file.txt)
    echo "$file_contents"
    

    In this example, the cat file.txt command reads the contents of the file.txt file and $(...) is a command substitution that captures the output of the command into the file_contents variable. Finally, the echo "$file_contents" command prints the contents of the file stored in the variable.