Input redirection within bash script

Hi,

when I try to redirect input and the command is described as a string within an array redirection does not work. why?

#!/bin/bash

dir=("tail < ./hello.txt")

tail < ./hello.txt #works
${dir[0]} #does not work

dir=("tail < ./hello.txt") is the equivalent of dir="tail < ./hello.txt"
You are assigning a string to a variable.
echo $dir will show you the string tail < ./hello.txt and not the execution of the command.

By the way, you do not need redirection. tail can read from the file without extra help as tail hello.txt

dir=$(tail hello.txt) will assign the result of executing the command into the variable dir.

dir="tail hello.txt"
eval $dir

That will evaluate the string tail hello.txt as a command.
Therefore,
eval ${dir[0]} will do the same.

1 Like

Hi thanks eval worked!

tail was just a placeholder for another program