My bash script receives a filename (or relative path) as a string, but must then read from that file. I can only read from a filename if I declare it as a literal directly in the script (without quotes)...which is impossible for arguments since they are implicitly strings to begin with. Observe:
a="~/test.txt"
#Look for it
if [[ -a $a ]] ; then
echo "A Found it"
else
echo "A Error"
fi
#Try to use it
while read line; do
echo $line
done < $a
b='~/test.txt'
#Look for it
if [[ -a $b ]] ; then
echo "B Found it"
else
echo "B Error"
fi
#Try to use it
while read line; do
echo $line
done < $b
c=~/test.txt
#Look for it
if [[ -a $c ]] ; then
echo "C Found it"
else
echo "C Error"
fi
#Try to use it
while read line; do
echo $line
done < $c
YIELDS:
A Error
./test.sh: line 10: ~/test.txt: No such file or directory
B Error
./test: line 12: ~/test.txt: No such file or directory
C Found it
Hello
As stated above, I can't pass a command line argument to the routines above since I get the same behavior that I get on the quoted strings.
$1
), then everything will work fine because the home directory expansion will have already been done before the script is called. – Wadmal~
is not quoted when his script is called from the command line or from another script. It's a different story when his script is called from some other program that passes a literal~
That would be a mistake that should be fixed in the other program; but if he want's to deal with such a case in his own script, he probably needseval
. – Flown