I want to know how I can put the contents of a variable in a text file. This variable is a string that I want the code with the base64 command.But this command only works on files. I wonder if there are other possibilities for this task
3 Answers
You don't need a text file, you can use this:
base64 <<< "$var"
or
echo "$var" | base64
Example:
var="abcde"
base64 <<< "$var"
results
YWJjZGUK
You can use Here Strings. It is supported by bash
, zsh
and other common shells.
Example: The grep
command only work with files but here we are passing a variable using Here Strings to be searched for pattern by grep
.
$ str="this is a test line"
$ grep -o "test" <<< "$str"
test
As far as your command is concerned you can use:
$ base64 <<< "$str"
Your question suggests that you want to base64 encode the content of a variable:
$ TEXT=test
$ ENCODED=$(echo "$TEXT" | base64)
$ echo "$ENCODED"
dGVzdAo=
-
2
"$var"
and${var}
are not the same. Choose the former to avoid word splitting and glob expansion.glenn jackman– glenn jackman2015-04-09 10:47:58 +00:00Commented Apr 9, 2015 at 10:47 -
echo
ing?echo $var > file.txt