how to loop through arguments in a bash script

rubo77

I would like to write a bash script with unknown amount of arguments.

How can I walk through these arguments and do something with them?

A wrong attempt would look like this:

#!/bin/bash
for i in $args; do 
    echo $i
done
Gilles 'SO- stop being evil'

There's a special syntax for this:

for i do
  printf '%s\n' "$i"
done

More generally, the list of parameters of the current script or function is available through the special variable $@.

for i in "$@"; do
  printf '%s\n' "$i"
done

Note that you need the double quotes around $@, otherwise the parameters undergo wildcard expansion and field splitting. "$@" is magic: despite the double quotes, it expands into as many fields as there are parameters.

print_arguments () {
  for i in "$@"; do printf '%s\n' "$i"; done
}
print_arguments 'hello world' '*' 'special  !\characters' '-n' # prints 4 lines
print_arguments ''                                             # prints one empty line
print_arguments                                                # prints nothing

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

bash arguments in a variable and then loop through

分類Dev

How to loop through arguments in Bash ($1 $2 $3 and so forth)?

分類Dev

Bash shell script FOR loop through files older than specific date

分類Dev

How do i write a bash script to loop through each directory in a folder and append all .txt files into a single file?

分類Dev

Multiple Fortran arguments in bash script

分類Dev

Bash Script - check arguments - files in for

分類Dev

how to loop through files that match a regular expression in a unix shell script

分類Dev

Loop Through List of Files in Bash

分類Dev

Java command line arguments through shell script

分類Dev

Passing command line arguments through Bash

分類Dev

Run bash script in WSL through powershell script

分類Dev

How to execute shell commands in a loop from within a bash script?

分類Dev

How to loop for 3 times in bash script when docker push fails?

分類Dev

How do I create a simple loop daemon bash script?

分類Dev

How to loop through loop in Ruby

分類Dev

ssh in bash script exits loop

分類Dev

bash + run script in the loop cycle

分類Dev

Propagate all arguments in a bash shell script

分類Dev

Bash script that runs a command with arguments and redirects

分類Dev

How to evaluate function arguments with for loop

分類Dev

how to pass many arguments to for loop?

分類Dev

.sh script will not let me loop through files

分類Dev

How to loop through files whose names contain vertical lines and spaces in bash

分類Dev

How to loop the items in script

分類Dev

How to loop through the same cell

分類Dev

How to Loop Through API Calls

分類Dev

How to Loop Through RadioField List

分類Dev

How to loop through result in sequelize

分類Dev

How to loop through urls in a template

Related 関連記事

  1. 1

    bash arguments in a variable and then loop through

  2. 2

    How to loop through arguments in Bash ($1 $2 $3 and so forth)?

  3. 3

    Bash shell script FOR loop through files older than specific date

  4. 4

    How do i write a bash script to loop through each directory in a folder and append all .txt files into a single file?

  5. 5

    Multiple Fortran arguments in bash script

  6. 6

    Bash Script - check arguments - files in for

  7. 7

    how to loop through files that match a regular expression in a unix shell script

  8. 8

    Loop Through List of Files in Bash

  9. 9

    Java command line arguments through shell script

  10. 10

    Passing command line arguments through Bash

  11. 11

    Run bash script in WSL through powershell script

  12. 12

    How to execute shell commands in a loop from within a bash script?

  13. 13

    How to loop for 3 times in bash script when docker push fails?

  14. 14

    How do I create a simple loop daemon bash script?

  15. 15

    How to loop through loop in Ruby

  16. 16

    ssh in bash script exits loop

  17. 17

    bash + run script in the loop cycle

  18. 18

    Propagate all arguments in a bash shell script

  19. 19

    Bash script that runs a command with arguments and redirects

  20. 20

    How to evaluate function arguments with for loop

  21. 21

    how to pass many arguments to for loop?

  22. 22

    .sh script will not let me loop through files

  23. 23

    How to loop through files whose names contain vertical lines and spaces in bash

  24. 24

    How to loop the items in script

  25. 25

    How to loop through the same cell

  26. 26

    How to Loop Through API Calls

  27. 27

    How to Loop Through RadioField List

  28. 28

    How to loop through result in sequelize

  29. 29

    How to loop through urls in a template

ホットタグ

アーカイブ