سؤال

I have a bash that should be run in this way:

./script.sh <arg1> <arg2> <arg3>...<argn>

I want to show these args in my bash:

<arg3> <arg4> ... <argn>

So I wrote this bash:

for (( i=1; i<=$#-3; i++ ))
do
    echo $((3+i))
done

but it shows me number of args.

How can I put # in order to see my real args?

Thanks

هل كانت مفيدة؟

المحلول 2

You can store all arguments in a BASH array and then use them for processing later:

args=( "$@" )
for (( i=2; i<${#args[@]}; i++ ))
do
    echo "arg # $((i+1)) :: ${args[$i]}"
done

نصائح أخرى

If you want to show arguments starting from arg3, you can simply use

echo "${@:3}" # OR
printf "%s\n" "${@:3}"

If you really want to show argument indices, use

for (( i=3; i < $#; i++)); do 
    echo $i
done

A minimal solution that displays the desired arguments without the math:

shift 2
for word
do
  echo ${word} 
done

I prefer @anubhava's solution of storing the arguments in an array, but to make your original code work, you could use eval:

for ((i=1;i<=$#;i++)); do
    eval echo "\$$i"
done

After your all good answers I found this solution that works well for my thread:

ARG=( $(echo "${@:3}") )
for (( i=1; i<=$#-3; i++ ))
do
    echo ${ARG[i]}
done
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top