Dimitris Delis
Dimitris Delis

Reputation: 55

Reading a text file into a variable in shell script

Hello I'm trying to save a text file(let's call it file.dat (it's a UTF-8 Unicode text) into a variable in a script I'm making. I want to call the file.dat like : ./myscript file.dat (or something similar). Having a command line in code in the form of variable=file.dat won't help.

I'm new to shell so I apologize if the question is not very specific

Upvotes: 0

Views: 8647

Answers (2)

banetl
banetl

Reputation: 199

Try something like this :

var=$(cat $1)

then var will contain the content of your file

Upvotes: 2

holygeek
holygeek

Reputation: 16205

Some thing like this should get you started:

#!/bin/bash

content=$(cat "$1")  # This is how you slurp the content of the file

echo "Content of $1:"
echo "$content"

Upvotes: 5

Related Questions