LINUX.ORG.RU

Вопрос по printf

 


0

0

Как выполнить printf с поддержкой всех, или максимумом, символов ('%' и какие там ему ещё надо указывать?), но с переводом строки?
Если делать printf '%s' "asdf\n", перенос строки показывается как символ.
В man 3 printf ничего не понял (C не знаю).

★★★★★

Ответ на: комментарий от Eddy_Em

О! не ставить \n после asdf я не додумался.

man printf не варик почитать?

там про '%s\n' и т.п. ни слова!
кроме % никаких символов специально не надо указывать?

teod0r ★★★★★
() автор топика
Ответ на: комментарий от teod0r

Да набери же ты man printf!

Там явно сказано, что первый аргумент — формат, а дальше уже аргументы, которые туда впихиваются. Причем, спецсимволы в %s, в отличие от сишечки, не раскрываются!

Eddy_Em ☆☆☆☆☆
()

man 3 printf это про printf(3) из glibc

если тебе интересен bash, набери

$ help printf
$ help printf
printf: printf [-v var] format [arguments]
    Formats and prints ARGUMENTS under control of the FORMAT.
    
    Options:
      -v var	assign the output to shell variable VAR rather than
    		display it on the standard output
    
    FORMAT is a character string which contains three types of objects: plain
    characters, which are simply copied to standard output; character escape
    sequences, which are converted and copied to the standard output; and
    format specifications, each of which causes printing of the next successive
    argument.
    
    In addition to the standard format specifications described in printf(1),
    printf interprets:
    
      %b	expand backslash escape sequences in the corresponding argument
      %q	quote the argument in a way that can be reused as shell input
      %(fmt)T output the date-time string resulting from using FMT as a format
            string for strftime(3)
    
    The format is re-used as necessary to consume all of the arguments.  If
    there are fewer arguments than the format requires,  extra format
    specifications behave as if a zero value or null string, as appropriate,
    had been supplied.
    
    Exit Status:
    Returns success unless an invalid option is given or a write or assignment
    error occurs.
emulek
()
Ответ на: комментарий от Eddy_Em

Да набери же ты man printf!

нет, это внешняя команда, в bash имеется встроенная, она (может) работать немного иначе.

AFAIK в /bin/sh только такая и есть (внешняя /bin/printf).

emulek
()
Ответ на: комментарий от emulek

не понял, что %b делает
а %s вообще нет

teod0r ★★★★★
() автор топика
Ответ на: комментарий от Eddy_Em

тест "\t\t\tтест"

почему, интересно, не работает, когда оба слова 'тест' в одних кавычках? не удобно же

teod0r ★★★★★
() автор топика
Ответ на: комментарий от teod0r

Потому что одинарные кавычки "защищают" содержимое.

Eddy_Em ☆☆☆☆☆
()
Ответ на: комментарий от Eddy_Em

потому что они раскрываются башом

printf '%s' $'asdf\nqwer'

об этом см. man bash раздел экранировка (QUOTING)

FeelUs
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.