Save text file in variable in bash

how can i read a text file and save it in a variable in bash? my code is here:

#!/bin/bash TEXT="dummy" echo "Please, enter your project name" read PROJECT_NAME mkdir $PROJECT_NAME cp -r -f /home/reza/Templates/Template\ Project/* $PROJECT_NAME cd $PROJECT_NAME/Latest TEXT = `cat configure.ac ` ## problem is here !!! CHANGED_TEXT=${TEXT//ProjectName/$PROJECT_NAME} echo $CHANGED_TEXT 
+6
source share
3 answers

The problem is that you have extra space. Assignment requires zero space between the = operator. However, with bash you can use:

 TEXT=$(<configure.ac) 

You will also want you to specify your variables to save new lines.

 CHANGED_TEXT="${TEXT//ProjectName/$PROJECT_NAME}" echo "$CHANGED_TEXT" 
+14
source

Try

 TEXT=`cat configure.ac` 

That should work.

Edit:

To clarify, the difference is between: placing after TEXT calls bash to try to find it as a command.

+3
source

To execute a command and return the result in a bash script to store in a variable, for example, you must write the inner command for the var = $ (command) command. And you should not specify spaces between var, '=' and $ (). Look at this

 TEXT=$('cat configure.ac') 

Now echo $TEXT returns the contents of the configure.ac file.

+1
source

Source: https://habr.com/ru/post/907675/


All Articles