Frage

As we can see from this Q&A, What does {0} mean when found in a string in C#?, C# uses a different approach than C/C++'s printf for String.Format, or Console.WriteLine, which allows the same parameter to be used at many places. E.g.,

String.Format("Fib: {0}, {0}, {1}, {2}, {0}", 1,  1+1, 1+2)

Is there any Linux command line printf tool that has the same benefit of allowing the same parameter to be used at many places?

War es hilfreich?

Lösung

Here's a one liner, using python (it's easy to write one using other languages).

% alias myprintf='python -c "import sys; print sys.argv[1].format(*sys.argv[2:])"'
% myprintf "a {0} is a {0} is a {1}" dog cat
a dog is a dog is a cat

In the alias command, I'm assuming you're using bash.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top