2 Replies - 814 Views - Last Post: 10 October 2012 - 03:23 AM

#1 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Bash Command Substitution

Posted 10 October 2012 - 01:49 AM

Hi

Can somebody explain what the hell is happening with this as it's driving me insane. The first 3 are entirely expected, but the latter confuses the crap out of me.

[root@rudi.test test]# echo hello
hello
[root@rudi.test test]# echo echo hello
echo hello
[root@rudi.test test]# (echo echo hello)
echo hello
[root@rudi.test test]# $(echo echo hello)
hello
[root@rudi.test test]# $(echo echo echo hello)
echo hello


Is $(echo built in to just put the command ran after it onto stdout? I don't see any other explanation for this behaviour whilst in a command substitution context ($(..)).

This post has been edited by RudiVisser: 10 October 2012 - 01:50 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Bash Command Substitution

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: Bash Command Substitution

Posted 10 October 2012 - 01:58 AM

There's nothing special about using echo inside $(). $(echo echo echo hello) executes the command inside the $(), then substitutes the result of executing the command for the $() and executes the resulting command - that's the same thing that would happen using any other command.

echo echo echo hello produces the string echo echo hello, so $(echo echo echo hello) is replaced with echo echo hello. This command is then executed and produces the output "echo hello".

This post has been edited by sepp2k: 10 October 2012 - 02:00 AM

Was This Post Helpful? 2
  • +
  • -

#3 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Bash Command Substitution

Posted 10 October 2012 - 03:23 AM

Eugh, now I feel like an idiot and that seems like the simplest thing ever.

Thank you for curing my stupidity :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1