bash - assign a variable with spaces in shell -
i have file has following text written -
hello h.i. b-y-e
i want read value variable. have defined function -
function read() { p=`cat $1`; echo "$p"; $2=`echo "$p"`; }
i following error -
hello h.i. b-y-e -bash: v=hello: command not found
however, when -
p=`cat $filename` text=`echo "$p"`
i have desired string text. can please explain difference in behaviour way acheive want do.
see wordsplitting means in shell context
all need store in variable is,
filecontent="$(<input-file)" printf "%s\n" "$filecontent" hello h.i. b-y-e
(or) if think not deserve 2 lines, use single-line as
printf "%s\n" "$(<input-file)"
(or) using function as
function getfilecontents() { local input=$1 printf "%s" "$(<input)" } newvariable="$(getfilecontents input-file)" printf "%s\n" "$newvariable" hello h.i. b-y-e
(and) if requirement bad enough pass variable function, like
unset newvariable function getfilecontents() { local input=$1 declare -n var=$2 var="$(<$input)" } getfilecontents file newvariable printf "%s\n" "$newvariable" hello h.i. b-y-e
Comments
Post a Comment