Linux Bash Shell Printf Command Examples

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-shell-printf-command-examples/
PINTEREST
PINTEREST
INSTAGRAM

Linux provides different commands to print given string or data into the terminla or specified place. printf is one of them which is similar to the C programming language printf()  function. Actually it is cloned from C printf function which provides similar features to write given string to the terminal in a structured manner.

Help

We will start by printing help information about printf . And as we will examine most of the options provided by printf in the next part of the tutorial.

Help
Help

Simply Print

We will start by simply printing given string. We will provide the string we want to print in a double quote. In this example we will print hi poftut .

Simply Print
Simply Print

String Format

We can use %s  in order to provide external strings to the given string. External strings will be added to the %s place. In this example we will provide poftut  as external string.

String Format
String Format

Multiple String Format

We can also provide multiple external string which will cause multiple print like a loop.

Multiple String Format
Multiple String Format

Double Quote

Because we use double quote in shell we need to have some special mechanism in order to print double quotes in string. We will use \" to print double quote properly.

$ printf “hi \”ismail\” ”

Double Quote
Double Quote

Backslash

As backslash is used as a helper we can use \\ as backslash like below.

LEARN MORE  Linux Grep OR, AND, NOT Operator and Logic Examples

$ printf “hi \ismail ”

Backslash
Backslash

Alert

If given terminal supports sound we can play a ping sound with \a which is named alert.

Backspace

Backspace is used to delete previously given string. We will use \b to specify backspace. In this example hi  will be deleted with two backspace.

Backspace
Backspace

No Further Output

We can stop output at the given position of the string we will use \c which is shortcut for cancel. In this example we will cut output after hi.

Escape

We can use \e for a escape secuence.

New Line

As we have all ready see that after the end of the string there is no new line which will cause terminal start to print to the same line of the string. We can use \n as new line which will start new line.

New Line
New Line

Carriage Return

We can use \r for carriage return key.

Horizontal Tab

Tabs provides some space between given position. Horizontal tab is used to place spaces in horizontal manner.

Horizontal Tab
Horizontal Tab

Vertical Tab

Vertical tab will place a tab in a vertical manner which will put given string to the next line.

Vertical Tab
Vertical Tab

Print Decimal Format

We can print given hex or octal value in decimal format with %d . In this example we will print hex number 0xF in decimal.

LEARN MORE  Python List or Array Tutorial With Examples

Print Decimal Format
Print Decimal Format

Print Hexadecimal Format

We can also print in Hex or Hexadecimal format with %X .We will also add 0x for prefix.

Print Hexadecimal Format
Print Hexadecimal Format

Print Octal Format

Octal format is used in 8 numbering system. We can use %o to print in octal format like below.

Print Octal Format
Print Octal Format

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/linux-bash-shell-printf-command-examples/
PINTEREST
PINTEREST
INSTAGRAM