# Input redirection within bash script

**URL:** <https://community.unix.com/t/input-redirection-within-bash-script/365682>\
**Category:** Shell Programming and Scripting\
**Created:** [March 20, 2017, 2:42pm UTC](https://community.unix.com/t/input-redirection-within-bash-script/365682 "2017-03-20T14:42:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![heinzel](https://community.unix.com/letter_avatar/heinzel/32/5_5575768a8748004e209b776fc1b2916d.png) [@heinzel](https://community.unix.com/u/heinzel)\
**Post date:** [March 20, 2017, 2:42pm UTC](https://community.unix.com/t/input-redirection-within-bash-script/365682/1 "2017-03-20T14:42:36Z")

</div>

Hi,

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

```nohighlight
#!/bin/bash

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

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

```

---

<div class="post-metadata">

**Author:** ![Aia](https://community.unix.com/user_avatar/community.unix.com/aia/32/917_2.png) [@Aia](https://community.unix.com/u/Aia)\
**Post date:** [March 20, 2017, 3:15pm UTC](https://community.unix.com/t/input-redirection-within-bash-script/365682/2 "2017-03-20T15:15:01Z")

</div>

> [@heinzel](#):
>
> Hi,
> 
> when I try to redirect input and the command is described as a string within an array redirection does not work. why?
> 
> ```plaintext
> #!/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.`

```plaintext
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.

---

<div class="post-metadata">

**Author:** ![heinzel](https://community.unix.com/letter_avatar/heinzel/32/5_5575768a8748004e209b776fc1b2916d.png) [@heinzel](https://community.unix.com/u/heinzel)\
**Post date:** [March 20, 2017, 3:57pm UTC](https://community.unix.com/t/input-redirection-within-bash-script/365682/3 "2017-03-20T15:57:21Z")

</div>

Hi thanks eval worked!

tail was just a placeholder for another program
