From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 5810 invoked from network); 19 Nov 2022 16:49:45 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 19 Nov 2022 16:49:45 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1668876585; b=FVpMiNCCORHEZe5OAk9LUB71aCoWAL0tMDD//rkA/db+neVua7RjhlgZ1sCTmQIcKyxjNL1oim TUnS0I/RmM2eFm6REoHZYlpfzZTWq5vHZ0+hzbBNWjgbzKXiETjxU4YIW0twwkuShLKoibe05f DAOwcdrV5WYQCcpKMfEHCUo9in+fqNjgbS55Y+ezTP8bks/BLJxBZp2ZAfJ2/yL08yAkLpovUp x59yuKy3kO13z6VeVyKs0WFjLL1gOPRrtyirOPJIotAdYU8nGGHzOfZhWq/8IvqG7Zt00Oc/u8 AZjh3VtZx2c+5n8vD/Y9O4NLasulRoNPOXtdf8cMjHmBqQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay6-d.mail.gandi.net) smtp.remote-ip=217.70.183.198; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1668876585; bh=z77fT6S74ToBcxVvnw3y5GnVP3SXjGzr0m3kf1lYVaw=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:DKIM-Signature; b=pqrfxXOvzYlXUS/+iArjpywepu4OcwxXkDbp5rII5Leuh+7B+guzngKVH17Y0efjTediP4xtpD X5Fl+GtbIAq7p0CZkfBHSgbCJgJc4yux6zBJFOsmR659KiUmP5qAtYn+AbEUqD8ExvBRzUotDn 9DlqOQmKLM6GqCZb/Q3xomR/dsiKbtv8embOgsbTkJRHbHWCq40a9nf//7EWCRWIP4eeTWjFBZ jwOncqA52dkeoaP+4wcanSbHAzdT/vYwLlPxM6jJXUnfkAkWpZPsfMR+nJFuVGgSI/y1D0AP1Z SCbaJfJuJ2TBW+KPskzIsq1DVkuLShEReudcajbL3/3t1A==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:In-Reply-To:Content-Transfer-Encoding :Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID; bh=/5VuGgJmTJjtvr1sExyZXESPDdxMHUdY4nF0ZtaBJjM=; b=UECkOxKN8lGEywqanCStk5tpwB 9JF5PlbseIu1jd9tFgKfjr9yvpwp7mD/zTfV22+6IEPm0PFfEq8A6/7IG/DN5fSIsMXYUkESIs03A QAiLOLs1UFrCrkpwLNBlJCacSIJGWWQPkTbndD1yD6NyNtWD5icbeQcb+m8Vpx8TaDQgC3hPwWV2f moDYjpPpfry1Excu1NlHoAQmoovw9vlnVtxiTTMJjsH6eC1FRHBSWY05PJ+kLMXaaW9jbuZs2XWkZ EREtSYKzhHsG2snMShkhbep2v8ek2lSvdH9q6Y5FfqJp1YdOmyWL4rgQBSXHtTWUVUigQCG90OSz9 J2mGJ9PQ==; Received: by zero.zsh.org with local id 1owR2G-0004E0-4x; Sat, 19 Nov 2022 16:49:44 +0000 Authentication-Results: zsh.org; iprev=pass (relay6-d.mail.gandi.net) smtp.remote-ip=217.70.183.198; dmarc=none header.from=chazelas.org; arc=none Received: from relay6-d.mail.gandi.net ([217.70.183.198]:60233) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1owR1T-0003XS-D7; Sat, 19 Nov 2022 16:48:56 +0000 Received: (Authenticated sender: stephane@chazelas.org) by mail.gandi.net (Postfix) with ESMTPSA id 99492C0003; Sat, 19 Nov 2022 16:48:53 +0000 (UTC) Date: Sat, 19 Nov 2022 16:48:52 +0000 From: Stephane Chazelas To: Ray Andrews Cc: Zsh Users Subject: Re: why is eval needed? Message-ID: <20221119164852.hwujmufa6hn5lotr@chazelas.org> Mail-Followup-To: Ray Andrews , Zsh Users References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: X-Seq: 28411 Archived-At: X-Loop: zsh-users@zsh.org Errors-To: zsh-users-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-users-request@zsh.org X-no-archive: yes List-Id: List-Help: , List-Subscribe: , List-Unsubscribe: , List-Post: List-Owner: List-Archive: 2022-11-19 06:34:30 -0800, Ray Andrews: >  In a script: > > local level='-L 2' > tree $level > #eval tree $level > > run it: > >     2 /aWorking/Zsh/Source/Wk 0 $ . testing >     tree: Missing argument to -L option. > > If I use 'eval' it's fine. We need eval when we need to evaluate some code stored as a string dynamically. For instance, in perl, you could do: $code_to_invoke_tree = 'system("tree", '; $code_to_represent_arguments_for_tree = '"-L", "2"'; $code = $code_to_invoke_tree . $code_to_represent_arguments_for_tree . ');' eval $code; We have $code containing system("tree", "-L", "2");. That's perl code which we evaluate with eval. That looks very silly doesn't it? Nobody's ever going to do that. Rather, you'd do: $cmd = "tree"; @args_for_tree = ("-L", "2"); system($cmd, @args_for_tree); It's exactly the same for shells except that the equivalent syntax of perl's: system("tree", "-L", "2"); to invoke tree with -L and 2 as arguments is: tree -L 2 In shell, you separate arguments with whitespace instead of ",". You don't need a system() function, because the shell's main job is to execute commands that's the main thing it does, it would get old quickly if had to use something like system() for everyone of them. You don't need quotes around those arguments, because everything is string in shells, since command arguments are strings and that's the one thing shells deal with. Now, you could also do: shell_code_to_run_tree='tree ' shell_code_to_represent_arguments_for_tree='-L 2' code=$shell_code_to_run_tree$shell_code_to_represent_arguments_for_tree eval $code but that would be equally silly. And like in perl you'd rather do: cmd=tree args_for_tree=(-L 2) $cmd $args_for_tree But you still need those -L and 2 to be two separate arguments, hence the list variable. If you do: cmd=tree arg='-L 2' $cmd $arg that's the same as: $cmd = "tree"; $arg = '-L 2'; system($cmd, $arg); And you're calling tree with only one "-L 2" argument while you want to call it with 2 arguments: -L and 2. -- Stephane