AhmFM
AhmFM

Reputation: 1782

read file from user input in bash and store in variable

Team,

looks simple but am unable to catch it. Am trying to read a file from user and store its contents into variable. My echo is working as its printing the file but my cat is not working.

read -p "specify the file to read nodes from: " file
echo $file

file_contents=$(cat $file | awk -F ',' '{print $1}')

for item in $file_contents
do
echo $item
done;

output

$HOME\backup\file.log

no such file
cat $HOME\backup\file.log

expected output

\home\users\backup\file.log

item1
item2
item3

Upvotes: 1

Views: 259

Answers (1)

AhmFM
AhmFM

Reputation: 1782

The file with should be entered with absolute path and not $HOME.

so providing whole file path worked for me.

if there is a way I can use $HOME. Please advise.

Upvotes: 1

Related Questions