0

I want to store number in file for example file number.txt contain 3242 I want to real this file and store its contant in variable for example variable number_var = number.txt so that number_var = 3242

Lucas Kauffman
  • 16,880
  • 9
  • 58
  • 93
Mohammad AL-Rawabdeh
  • 1,612
  • 12
  • 33
  • 54

3 Answers3

3
number_var=$(cat number.txt)
Ignacio Vazquez-Abrams
  • 45,939
  • 6
  • 79
  • 84
3

The backtick is your friend:

[madhatta@risby tmp]$ cat foo
1234
[madhatta@risby tmp]$ fred=`cat foo`
[madhatta@risby tmp]$ echo $fred
1234

You had better be pretty sure the file is a one-liner, though; if you import the whole of /boot/vmlinuz into a shell variable, you may start putting pressure on memory!

MadHatter
  • 79,770
  • 20
  • 184
  • 232
2

In Bash, ksh and zsh:

number_var=$(<number.txt)
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151