I don’t know how to execute a command with a variable and get the result of this.
I have many .h and .c files and I need convert each from ISO-8859 to UTF-8.
So I make:
ls | grep "\.[ch]" | xargs myscript.sh
And in my script, the filename is in variable $1. Now I need to perform
iconv -f ISO-8859 -t UTF-8 $1
and store result of this, because iconv prints to stdout.
result=`iconv -f ISO-8859 -t UTF-8 $1`
echo $result
This seems to be not working, because it gives me some mismatch instead of converted $1.
If you need to do some kind of transformation on the data first, you can “capture” output with the following syntax:
There is a gotcha here as well: if you are going to be storing large amounts of data with potential whitespace or other meddlesome characters in it, be sure to always quote the variable (
"$result"instead of$result) to ensure it gets treated as a single string.