a simple for loop in bash and zsh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting a simple for loop in bash and zsh
# 1  
Old 10-24-2011
a simple for loop in bash and zsh

Hi! I am just starting to learn scripting.
I am trying a simple script in bash and zsh
I have two questions:

First: Why zsh does not expand the var M? What I am doing wrong?
Code:
localhost galanom # bash -c 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done'
<m1>
<m2>
<m3>
<m4>
<m5>
localhost galanom # zsh -c 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done'
<m1 m2 m3 m4 m5>
localhost galanom #

Second, if I use newlines or nothing after do statement, everything is ok. But if I use "do;" then:

Code:
localhost galanom # zsh -c 'M="m1 m2 m3 m4 m5"; for x in $M; do; echo "<$x>"; done'
<m1 m2 m3 m4 m5>
localhost galanom # bash -c 'M="m1 m2 m3 m4 m5"; for x in $M; do; echo "<$x>"; done'
bash: -c: line 0: syntax error near unexpected token `;'
bash: -c: line 0: `M="m1 m2 m3 m4 m5"; for x in $M; do; echo "<$x>"; done'
localhost galanom #

If anyone can help me, I would appreciate it!
Thank you!
# 2  
Old 10-25-2011
"Why zsh does not expand the var M? What I am doing wrong?"

Zsh supports word splitting but the option is not turned on by default.
Code:
zsh --shwordsplit -c 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done'

#or

zsh -yc 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done'

This User Gave Thanks to xbin For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

MacOS 10.15 (Catalina) switches from bash to zsh

Interestingly Apple has decided to switch the default shell for new users from bash to zsh in MacOS Catalina (10.15) Use zsh as the default shell on your Mac - Apple Support Another interesting fact is that Catalina also comes with /bin/dash (5 Replies)
Discussion started by: Scrutinizer
5 Replies

2. Shell Programming and Scripting

Help with simple bash script involving a loop

Dear unix wizards, I'd be very grateful for your help with the following. I have a hypothetical file (file.txt) with three columns: 111 4 0.01 112 3 0.02 113 2 0.03 114 1 0.04 115 1 0.06 116 2 0.02 117 3 0.01 118 4 0.05 Column 2 consists of pairs of integers from 1 to 4 (each... (5 Replies)
Discussion started by: aberg
5 Replies

3. Shell Programming and Scripting

Run bash command inside zsh script

Hi, I would like to run following code in bash inside a zsh script. (In this case is output unfortunately very different if you run it in zsh). I tried to put "bash" in front of the code but I obtained following error message "bash: do: No such file or directory " eve though I merged the whole... (7 Replies)
Discussion started by: kamcamonty
7 Replies

4. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

5. Shell Programming and Scripting

Using variables created sequentially in a loop while still inside of the loop [bash]

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends. As an example I've written a script called question (The fist command is to show what is the contents of the... (2 Replies)
Discussion started by: DeCoTwc
2 Replies

6. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

7. Shell Programming and Scripting

Simple loop in Bash

Hi, I want to do a simple loop where I have one column of text in a file and I want the loop to read each line of the file and do a simple command. The text file will be something like this: hostname1 hostname2 hostname3 hostname4 I am using Bash and have already come up with this to... (1 Reply)
Discussion started by: BrewDudeBob
1 Replies

8. Shell Programming and Scripting

From bash to csh and zsh

... Am I glad to find this forum (and vBulletin too, nice!).. OK, here's my issue. I have been handballed a bash script, not pretty but functional. I need to change to csh and zsh. For the csh I have the basics (e.g., such as change if/fi to if/endif, quote the variables, and bracket commands).... (10 Replies)
Discussion started by: lev_lafayette
10 Replies

9. Shell Programming and Scripting

Simple bash for loop problem

I'm just trying to make a script that runs in command line to echo each line in a text file. Everything i found on google is telling me to do it like this but when I run it it just echos removethese.txt and thats it. Anyone know what im doing wrong? for i in removethese.txt; do echo $i; done ... (4 Replies)
Discussion started by: kingdbag
4 Replies

10. Solaris

Missing init files for zsh and bash

I change my default shell to zsh but can't find the init files, .zshrc and .zlogin from /export/home and /home. The other shells init files are there:.cshrc, .profile and .login. Am I suppose to use these as templates? Also, bash_history is there but not zsh_history although zsh do keep a... (2 Replies)
Discussion started by: maag
2 Replies
Login or Register to Ask a Question