0

I'm trying to list script's argument by printf function. Arguments are counted by iteration of $i. What should be in printf function? I need something like

eval echo \$$i

but in printf function.

Edit: Have while cycle with iteration of $i and among other code, I have

printf "%s" $i

But, instead of $i, I need some code, that shows me value of argument. In my case, it is name of file, and I need list them. One file in one iteration.

4
  • Normally, you do that with i=1;for arg in "$@"; do echo "$i: $arg"; ((i++)); done, iterating through the list of arguments. Is there any reason why you can't do that too? Commented Mar 22, 2014 at 22:23
  • I cant use echo, because I'm formating the text, this is not whole printf I use, but only problem I have Commented Mar 22, 2014 at 22:26
  • 1
    OK; don't use echo; use: i=1;for arg in "$@"; do printf "%s\n" "$i: $arg"; ((i++)); done. That produces the same thing as the echo does. You can vary the formatting to suit yourself. See also my answer. It would also help if you showed enough context for what you want to do and what you have tried that we can see how to help you properly, rather than having to guess what it is you want us to help you with. Commented Mar 22, 2014 at 22:33
  • If you want to render nonprintable characters in such a way as to make them readable, you might consider printf '%q'. Be aware that this is a bashism, unavailable in POSIX printf. Commented Mar 22, 2014 at 23:01

4 Answers 4

2

As noted in a comment, you normally do that with a loop such as:

i=1
for arg in "$@"
do
    echo "$i: [$arg]"
    ((i++))
done

(If echo isn't allowed, use printf "%s\n" … where the is whatever would have followed echo.)

You might also use indirect expansion to avoid the use of eval:

for i in 1 2 3 4; do echo "$i: [${!i}]"; done

You can generalize that with:

for i in $(seq 1 $#); do echo "$i: [${!i}]"; done

or

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

For example, given:

set -- a b 'c  d' '  e  f  '

all the loops produce the output:

1: [a]
2: [b]
3: [c  d]
4: [  e  f  ]

The square brackets are merely there to delimit the argument values; it allows you to see the trailing blanks on the fourth line of output.

You might also be able to use:

printf "[%s]\n" "$@"

to get:

[a]
[b]
[c  d]
[  e  f  ]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that helps me, epsecially the part with "$i: [${!i}]". Thanks for help
1

It is not very clear what you are asking for, but this will list the arguments passed to the script:

while [ $# -gt 0 ]; do
    printf "%s\n" "$1"
    shift
done

5 Comments

You could write: printf "%s\n" "$@" and get the same result.
Aha, definitely nicer!
Sorry, I edited my main post. English is not my native language, so I hope it is a clearer for you
Well if I get your edit correctly, the while loop with the shift should do what you want.
Right, added the qoutes.
0

I guess what you are asking for is how to make the indirect reference to positional parameter i (i containing the position):

print "%s\n" ${!i}

2 Comments

Thanks, that is it, similar answer contain answer by Jonathan.
This is buggy -- an argument with "hello world" would render as two lines, one with hello and the other with world.
0

To get your arguments in such a way that they can be fed back into the shell with the exact same value, the following bash extension can be used:

printf '%q ' "$@"; printf '\n'

This works even for rather unusual cases. Let's say that one of your arguments contains a newline:

./your-script 'hello
world' 'goodbye world'

This will be represented in the printf output:

$'hello\nworld' goodbye\ world

...with something you can use again in the shell:

$ echo $'hello\nworld' goodbye\ world
hello
world goodbye world

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.