Home > Blockchain >  Read a variable stored into another file in bash
Read a variable stored into another file in bash

Time:03-09

I want to retrieve the contents of a variable stored in another file.

  1. my file content: file.txt
text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt
     ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco 
     laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in 
            voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat 
     cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum"
  1. my script : script.sh
#!/bin/bash
my_var=$(grep "^text=" file.txt | awk -F"=" '{print $2}' )
echo "$my_var"

Now when I run my script It just retrieves the first line of the variable text and I want to have the whole content of the variable

CodePudding user response:

Assign the entire contents of the file to a variable, then use a parameter expansion operator to remove the text= prefix.

my_var=$(< file.txt)
echo "${my_var#*=}"

${my_var#*=} expands to the value of $my_var with a prefix that matches the wildcard *= removed.

  •  Tags:  
  • bash
  • Related