Transfered from Linux Config:
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:
If you intend to do something with your arguments within a script you can try somethign simple as the following script:Code:#!/bin/bash echo $@
Next we have some script which are doing the same as the previous bash script but employ different approach:Code:#!/bin/bash for i; do echo $i done
Let's print all bash arguments using shift:Code:#/bin/bash for i in $*; do echo $i done
Code:#!/bin/bash while (( "$#" )); do echo $1 shift done
Or we can do something obscure like this to print all bash arguments:
Code:#/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


Reply With Quote
