linux shell loop through all inputs except last 1

linux shell loop through all inputs except last

for x in "${@:1:$# - 1}" ; do 
    echo do something with "$x"
done

Here is what the above code is Doing:
1. The first line is a shebang. It tells the shell what program to use to run the script.
2. The second line is a comment. It’s ignored by the shell.
3. The third line is a variable assignment. It sets the variable $last to the last argument passed to the script.
4. The fourth line is a for loop. It loops over all the arguments passed to the script except the last one.
5. The fifth line is a command. It prints the current argument.

Similar Posts