الفلاتر
طريقة البحث
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • "كلمتان" تجد العبارة بالضبط.
  • --name أو -x تجد خيارًا.
  • #كلمة تبحث داخل الأوامر فقط.
  • @grep تجد الدروس والمعامل التي تستخدم grep.
  • الفلاتر: cat:network · in:labs · level:rhcsa · ch:8 · root:yes. ضع ! قبل كلمة لاستبعادها. OR بين الكلمات تعني إحداها.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث

printf

Print formatted text: columns, numbers, padding — more exact than echo.

المستوى
لينكس اليومي
المجموعة
الطرفية والمساعدة
يحتاج root
لا
الحزمة
bash (built-in) / coreutils

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

ماذا يفعل

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

الأكثر استخدامًا

-v VAR
Store the result in a variable instead of printing (bash).
--help, --version
Help / version (coreutils printf).

أمثلة 10

$ مستخدم عادي# بصلاحية root (أو مع sudo)

  1. 1Text with a new line

    printf "Hello %s\n" "$USER"
    Hello student

    Add \n yourself.

  2. 2Aligned columns

    printf "%-10s %5s\n" USER UID root 0 lisa 1001
    USER         UID
    root           0
    lisa        1001

    The format repeats for each pair.

  3. 3Numbers with decimals

    printf "%.2f GiB\n" 3.14159
    3.14 GiB

    Rounded.

  4. 4Leading zeros

    for i in 1 2 10; do printf "server%02d\n" $i; done
    server01
    server02
    server10

    Names for many hosts.

  5. 5Write a file with lines

    printf 'line1\nline2\n' > /tmp/two-lines.txt

    Safer than echo -e.

  6. 6Date without calling date

    printf '%(%F %T)T\n' -1

    -1 = now (bash builtin).

  7. 7Store in a variable

    printf -v STAMP '%(%Y%m%d)T' -1; echo "backup-$STAMP.tgz"

    No subshell needed.

  8. 8Hex of a number

    printf "%x\n" 255
    ff

    Also %o for octal.

  9. 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.

  10. 10A simple table from a file

    while IFS=: read -r U _ ID _; do printf "%-15s %6d\n" "$U" "$ID"; done < /etc/passwd | head -3

    Neat report.

رموز الخروج

0 :: Done. 1 :: Error (like a wrong number format).

الدليل الكامل

هذه الصفحة تشرح الأمر بكلمات بسيطة. الدليل الأصلي فيه كل التفاصيل.

في الطرفية: man printf