From the course: Learning Linux Shell Scripting

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

The for loop

The for loop - Linux Tutorial

From the course: Learning Linux Shell Scripting

Start my 1-month free trial

The for loop

- [Instructor] Let's take a look at the for loop. From the terminal type touch for.sh, change mode 755 for.sh, and atom for.sh. And we'll do our shebang. This time we'll take some parameters from the user. We could take the parameters individually using $1, $2 and so on but there is another special symbol, dollar sign at sign. So, here we're gonna say NAMES and we're gonna say equals $@. $@ holds all of the parameters the user typed in one array. We can then use this with the loop to iterate over each parameter one at a time. So, we say for NAME in $NAMES and then we say do and we're gonna have a done and then in between here we'll say echo hello $NAME, so this will print hello for each name that we typed in. Then we'll say echo for loop terminated. And exit 0. Do a control S to save, go back to the terminal and we'll type in some names, so we're gonna say for.sh and we'll type in a few names and I'll say Stacy, Tracy and Lacy. And we get hello Stacy, hello Tracy and hello Lacy. One…

Contents