1

My system is CentOS 6.5 When I want to use backtick to run the commands in filename, i got the result below:

the file's content is below:

[liu-uil@~ 15:54:16]$cat test
echo 1;
echo 2;
echo 3;

[liu-uil@~ 15:54:18]$`cat test`
1; echo 2; echo 3;
[liu-uil@~ 15:54:24]$

the commands after the first echo are all treated as text plain, I don't know why? Could somebody kindly explain this to me? Thank you very much!

liu
  • 37
  • 5
  • What are you actually trying to accomplish? Whatever your end goal is, `cat` in backticks is probably not what you should be using. – tripleee Dec 08 '15 at 09:22
  • I just want to learn a little more about the backticks and solve this problem :) (a little nerd ) – liu Dec 08 '15 at 09:29

2 Answers2

2

Command substitution is one of the expansions. Expansions happen when the command line was already split into commands, it's too late to create new commands.

You can use

eval `cat 1`

to call the shell parser again to split the string into commands and run them.

choroba
  • 231,213
  • 25
  • 204
  • 289
  • Aha, I got it , now I know the reason why it doesn't work, but eval 'cat test' and \`\`cat test\`\` can neither get the expected result – liu Dec 08 '15 at 09:15
0

Only the first word in the result of a backtick command is interpreted as a command. Remaining text is the argument list.

If you want to run commands in a file, you don't need backticks, you need the dot (.) command:

[liu-uil@~ 15:54:16]$. test
mouviciel
  • 66,855
  • 13
  • 106
  • 140