gpt-3.5-turbo-0125· openai
Suzhen/CodeChat
Linux bash, store contenue of file to variable
- User
Linux bash, store contenue of file to variable
- Assistant
To store the contents of a file in a Linux bash script to a variable, you can use the
catcommand followed by command substitution. Here's an example:file_contents=$(cat file.txt) echo "$file_contents"In this example, the
cat file.txtcommand reads the contents of thefile.txtfile and$(...)is a command substitution that captures the output of the command into thefile_contentsvariable. Finally, theecho "$file_contents"command prints the contents of the file stored in the variable.