How do I print all arguments submitted on a command line from a bash script?
Last Updated on Friday, 01 April 2011 18:22
Question:
How do I print all arguments submitted on a command line from a bash script?
Answer:
There are couple ways how to print bash arguments from a script. Try some scripts below to name just few.
In this first script example you just print all arguments:
#!/bin/bash echo $@
If you intend to do something with your arguments within a script you can try somethign simple as the following script:
#!/bin/bash for i; do
echo $i done
Next we have some script which are doing the same as the previous bash script but employ different approach:
#/bin/bash for i in $*; do echo $i done
Let's print all bash arguments using shift:
#!/bin/bash while (( "$#" )); do echo $1 shift done
Or we can do something obscure like this to print all bash arguments:
#/bin/bash # store arguments in a special array args=("$@") # get number of elements ELEMENTS=${#args[@]} # echo each element in array # for loop for (( i=0;i<$ELEMENTS;i++)); do echo ${args[${i}]} done
















