0

There is something i do not understand with strings in bash:

Look at this script:

#!/bin/bash
tmp="ls"
"$tmp"

This script executes ls command and display result in the console.

Now look at this script:

#!/bin/bash
tmp="ls > out.txt"
"$tmp"

This second script does not execute ls and displays this error:

 line 3: ls > out.txt: command not found

I just want to understand. I do not want to understand how to run ls command. I want to understand why the first script works and not the second.

Thanks

Bob5421
  • 7,757
  • 14
  • 81
  • 175
  • 1
    [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050) describes the "why". – Charles Duffy Mar 28 '20 at 15:29
  • 1
    ...the very high-level explanation is that bash is not like m4 -- it has an actual evaluation model that's considerably more involved than just replacing strings with other strings and then running those strings through the same parser as the original. And a good thing, too -- it would be impossible to write secure code handling untrusted data were it otherwise. (Using `eval` defeats those security measures, and shouldn't be used unless you *really* know what you're doing; see [BashFAQ #48](https://mywiki.wooledge.org/BashFAQ/048)). – Charles Duffy Mar 28 '20 at 15:30
  • https://mywiki.wooledge.org/BashParser is a good place to start for an overview on the parsing-and-execution model. – Charles Duffy Mar 28 '20 at 15:31

0 Answers0