Reputation: 31
How do I set a Bash variable in a shell script in redhat Linux to the contents of a file, which is only one line, and then add some text after it.
For example:
echo "Version 1.2.3.4" > $TMPDIR/devver
env=dev
export ${env}_result="`cat $TMPDIR/${env}ver` (No response - version is from cache)"
echo $dev_result
I want the output to be:
Version 1.2.3.4 (No response - version is from cache)
Instead, I get a newline after the version, like this:
Version 1.2.3.4
(No response - version is from cache)
Upvotes: 3
Views: 1664
Reputation: 247042
Use a bash builtin:
export ${env}_result="$(< $TMPDIR/${env}ver) (No response - version is from cache)"
echo "$dev_result"
Documented here:
"The command substitution $(cat file)
can be replaced by the equivalent but faster $(< file)
."
Upvotes: 2
Reputation: 8847
the utility tr
may help:
echo "Version 1.2.3.4" | tr '\n' ' '> $TMPDIR/devver
env=dev
export ${env}_result="`cat $TMPDIR/${env}ver` (No response - version is from cache)"
echo $dev_result
tested :) it works.
Upvotes: 1
Reputation: 7698
Your problem isn't in the variable setting it is in the file creation. echo appends a newline to the file contents. So use
echo -n "Version..."
and things should work.
Upvotes: 3