هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
ماذا يفعل
printf FORMAT ARGUMENTS fills the arguments into the format. %s = text, %d = whole number, %f = decimal number, \n = new line. printf does not add a new line by itself. If there are more arguments than placeholders, the format is used again.
طريقة الكتابة
printf [-v VAR] FORMAT [ARGUMENT]...الأجزاء الرئيسية
%s- Text.
%d,%i- Whole number.
%f,%.2f- Decimal number (with 2 decimals).
%x,%o- Hex / octal.
%5s,%-10s- Width 5 (right aligned) / width 10 left aligned.
%05d- Pad with zeros.
%q- Quote for the shell (bash).
%(FORMAT)T- Date and time (bash), like %(%F)T.
%%- A % sign.
\n,\t,\\- New line, tab, backslash.
الخيارات 2
الأكثر استخدامًا
لا يوجد شيء يطابق هذا الفلتر.
أمثلة 10
$ مستخدم عادي# بصلاحية root (أو مع sudo)
1Text with a new line
printf "Hello %s\n" "$USER" Hello studentAdd \n yourself.
2Aligned columns
printf "%-10s %5s\n" USER UID root 0 lisa 1001 USER UID root 0 lisa 1001The format repeats for each pair.
3Numbers with decimals
printf "%.2f GiB\n" 3.14159 3.14 GiBRounded.
4Leading zeros
for i in 1 2 10; do printf "server%02d\n" $i; done server01 server02 server10Names for many hosts.
5Write a file with lines
printf 'line1\nline2\n' > /tmp/two-lines.txtSafer than echo -e.
6Date without calling date
printf '%(%F %T)T\n' -1-1 = now (bash builtin).
7Store in a variable
printf -v STAMP '%(%Y%m%d)T' -1; echo "backup-$STAMP.tgz"No subshell needed.
8Hex of a number
printf "%x\n" 255 ffAlso %o for octal.
9Quote a value safely for the shell (security)
printf '%q\n' "file name; rm -rf /" file\ name\;\ rm\ -rf\ /Safe to paste into a command.
10A simple table from a file
Neat report.
لا يوجد شيء يطابق هذا الفلتر.
رموز الخروج
0 :: Done. 1 :: Error (like a wrong number format).