Beginner"s question on command substitution

Beginner"s question on command substitution

am 14.10.2007 02:17:14 von Eze

Greetings to all,

I'm just getting started with shell scripting (using bash). Could
somebody please tell me why

$(cat myscript)

works when myscript is just

ls

but not when it's

for x do echo $x; done

even though in both cases ./myscript runs without problems? Clearly
I'm missing something about the internals of command substitution.

Thanks!

Eze

Re: Beginner"s question on command substitution

am 14.10.2007 02:47:10 von Janis Papanagnou

Eze wrote:
> Greetings to all,
>
> I'm just getting started with shell scripting (using bash). Could
> somebody please tell me why
>
> $(cat myscript)
>
> works when myscript is just
>
> ls
>
> but not when it's
>
> for x do echo $x; done
>

I understand you mean that either ls or the for-loop is in the file
myscript, right? First of all you should clarify what "works" means!
What do you expect?

The latter construct should not print anything if just called as you
have shown above.

for x is a shortcut for for x in "$@"

and "$@" is unset in your script. What you might want to do is maybe

for x in * ; do ...

or call myscript with arguments

./myscript *

If you fixed that then the embedding it into $(...) should "work".

Janis


> even though in both cases ./myscript runs without problems? Clearly
> I'm missing something about the internals of command substitution.
>
> Thanks!
>
> Eze
>

Re: Beginner"s question on command substitution

am 14.10.2007 03:52:09 von Eze

Thanks for your reply, Janis. Don't pay too much attention to this
thread... I think I'm still a bit too lost and may re-post in the
future. I did mean the "$@" shortcut in the for loop, and all I can
articulate is that I'm puzzled by the error I get,

-bash: for: command not found

How come bash cannot "find" for? Again, the best explanation may just
be RTFM.

Thanks again,

Eze

Re: Beginner"s question on command substitution

am 14.10.2007 04:38:29 von Barry Margolin

In article <1192321034.029614.223330@y27g2000pre.googlegroups.com>,
Eze wrote:

> Greetings to all,
>
> I'm just getting started with shell scripting (using bash). Could
> somebody please tell me why
>
> $(cat myscript)
>
> works when myscript is just
>
> ls
>
> but not when it's
>
> for x do echo $x; done
>
> even though in both cases ./myscript runs without problems? Clearly
> I'm missing something about the internals of command substitution.

For the same reason that

FOO=ls
$FOO

works, but

FOO='for x do echo $x; done'
$FOO

doesn't. Variable and command substitution takes place AFTER parsing
for shell built-ins and metacharacters.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

Re: Beginner"s question on command substitution

am 14.10.2007 14:01:51 von Janis Papanagnou

Eze wrote:
> Thanks for your reply, Janis. Don't pay too much attention to this
> thread... I think I'm still a bit too lost and may re-post in the
> future. I did mean the "$@" shortcut in the for loop, and all I can
> articulate is that I'm puzzled by the error I get,
>
> -bash: for: command not found
>
> How come bash cannot "find" for? Again, the best explanation may just
> be RTFM.

If you want the shell to perform another parsing iteration to interpret
the for statement in the file you can use 'eval'...

eval $( cat myscript )


Janis

>
> Thanks again,
>
> Eze
>

Re: Beginner"s question on command substitution

am 14.10.2007 17:10:13 von Eze

Thanks, Barry and Janis; it makes more sense now. I still have a long
way to go!