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

3
  • 1
    BashFAQ #50 describes the "why". Commented Mar 28, 2020 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). Commented Mar 28, 2020 at 15:30
  • mywiki.wooledge.org/BashParser is a good place to start for an overview on the parsing-and-execution model. Commented Mar 28, 2020 at 15:31

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.